Python 图片库无法转换为 pdf

Python Image Library can't convert to pdf

我有一个用 PILLOW 将 bmp 转换为 pdf 的功能,这个脚本我有非编译版本和编译版本 (.exe)。在第一个中它工作正常,但在第二个 PILLOW 中抛出异常('PDF')。具体失败在.save() 路径和带扩展名的文件名正确。

from PIL import Image
def bmp2pdf(self, file):
    ''' Convert a bmp file to PDF file, and delete old bmp file '''
    img = Image.open(file)
    output = file.replace('.bmp', '.pdf')
    try:
        img.save(output, "PDF", resolution=100.0)
        remove(file)
    except Exception as err:
        print(err)

在编译版本中输出是:

'PDF'

谢谢。

关注此 code.It 作品。 3行代码。

from PIL import Image def bmp2pdf(self,path): img = Image.open(path) img.save('image.pdf','pdf')

我得到了一个名为 image.pdf 的文件,其中包含图像。

我必须在我的设置中添加以生成 .exe 我应该导入 PIL 而不是 PIL.IMAGE,因此整个模块已加载并且 pdf 功能可用

我正在使用 cx_freeze:

'Packages': [
    'PyQt5.uic',
    "...",
    'PIL',
]