使用PIL保存模式错误将png转换为pdf

时间:2018-06-08 14:49:55

标签: python-3.x python-imaging-library

我试图将png文件转换为pdf文件。 PIL似乎是这样做的,但我在运行时遇到错误(cannot save mode RGBA

代码:

import os
import PIL
from PIL import Image

path = 'E:\path_to_file'
filename = '15868799_1.png'
fullpath_filename = os.path.join(path, filename)
im = PIL.Image.open(fullpath_filename)
newfilename = '15868799.pdf'
newfilename_fullpath = os.path.join(path, newfilename)
PIL.Image.Image.save(im, newfilename_fullpath, "PDF", resoultion=100.0)

错误:

 File "C:\Python\lib\site-packages\PIL\PdfImagePlugin.py", line 148, in _save
 raise ValueError("cannot save mode %s" % im.mode)
 ValueError: cannot save mode RGBA

1 个答案:

答案 0 :(得分:0)

首先需要convert your PNG from RGBA to RGB

示例代码:

File: