YasserKhalil
YasserKhalil

Reputation: 9568

Convert image to pdf fail by unknow file extension

I have the following code that loops through all the images with any extension in multiple folders then to convert those images to pdf I am stuck at the last line of saving the pdf file

from PIL import Image
import os

path = 'MAIN'
ext = ['.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff']

for root, dirs, files in os.walk(path):
    for file in files:
        if(file.endswith(tuple(ext))):
            sFilePath = os.path.join(root,file)
            print(sFilePath.split('.')[0])
            img = Image.open(sFilePath)
            img = img.convert('RGB')
            img.save(os.path.join(sFilePath.split('.')[0], '.pdf'))

Another point I need to add is to delete the image file after converting it to pdf

@martineau as for your awesome solution you provided, I tried to change the way I convert the image to pdf using img2pdf Here's my try

import img2pdf
with open(path_object.parent / (path_object.stem + '.pdf'), 'wb') as f:
    f.write(img2pdf.convert(str(path_object)))

with the images with the alpha channel, I got this message Image contains an alpha channel which will be stored as a separate soft mask (/SMask) image in PDF.

This is snaphot of the error message related to the alpha-channel images enter image description here

Upvotes: 0

Views: 1360

Answers (2)

martineau
martineau

Reputation: 123531

Another yet way to do things that makes use the pathlib module which allows file and folder paths to be tried as objects and manipulated via an object-oriented programming paradigm or model — which often results in very concise code:

from pathlib import Path
from PIL import Image

path = Path('./images')
exts = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff'}

for path_object in path.glob('**/*'):
    if path_object.is_file() and path_object.suffix in exts:
        img = Image.open(path_object)
        img = img.convert('RGB')
        img.save(path_object.parent / (path_object.stem+'.pdf'))

Upvotes: 1

martineau
martineau

Reputation: 123531

You can do it by using the os.path.splitext() function to get base part of the image's filename, and then combine that plus the .pdf extension you want using os.path.join(). (Note that converting it to RGB is not the same as converting it to PDF format.)

from PIL import Image
import os

path = './'
ext = ['.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff']

for root, dirs, files in os.walk(path):
    for file in files:
        if file.endswith(tuple(ext)):
            src_image_path = os.path.join(root, file)
            base_name = os.path.splitext(file)[0]
            img = Image.open(src_image_path)
            img = img.convert('RGB')
            img.save(os.path.join(root, base_name+'.pdf'))

Upvotes: 1

Related Questions