简体   繁体   English

未知文件扩展名将图像转换为 pdf 失败

[英]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我有以下代码循环遍历多个文件夹中具有任何扩展名的所有图像,然后将这些图像转换为 pdf 我被困在保存 pdf 文件的最后一行

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还有一点我需要补充的是将图片文件转换为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 @martineau 至于你提供的很棒的解决方案,我试图改变我使用 img2pdf 将图像转换为 pdf 的方式这是我的尝试

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.对于带有 alpha 通道的图像,我收到此消息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这是与 alpha 通道图像相关的错误消息的快照在此处输入图像描述

You can do it by using theos.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() .您可以使用os.path.splitext() function 获取图像文件名的基本部分,然后使用os.path.join()将其与您想要的扩展名.pdf组合起来。 (Note that converting it to RGB is not the same as converting it to PDF format.) (请注意,将其转换为RGB将其转换为 PDF 格式不同。)

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'))

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:另一种方法是使用pathlib模块,它允许将文件和文件夹路径作为对象进行尝试,并通过面向对象的编程范例或 model 进行操作——这通常会产生非常简洁的代码:

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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM