我有一个函数将bmp转换为带有PILLOW的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'
THX。
答案 0 :(得分:1)
请遵循此代码。它有效。 3行代码。
from PIL import Image
def bmp2pdf(self,path):
img = Image.open(path)
img.save('image.pdf','pdf')
我有一个名为image.pdf的文件,里面有图片。