I am tring to convert images in to a PDF. I have this code:
def convertToPDF(folder, PDFname, deleteLast=False):
'''
converts all images in a folder to a PDF.
'''
imageList = []
for filename in glob.glob(folder + f'/*'):
image=Image.open(filename)
image.convert('RGB') # convert to RGB
imageList.append(image)
imageList[0].save(f'./' + PDFname + '.pdf',save_all=True, append_images=imageList[1:]) # take the first image and add everything else
and I get this error sometimes:
File "c:\Users\felix\OneDrive\Desktop\Programmieren\TelegrammBotProjekt\manganeloAPI.py", line 195, in convertToPDF
imageList[0].save(f'./' + PDFname + '.pdf',save_all=True, append_images=imageList[1:]) # take the first image and add everything else
File "C:\Users\felix\Anaconda3\lib\site-packages\PIL\Image.py", line 2151, in save
save_handler(self, fp, filename)
File "C:\Users\felix\Anaconda3\lib\site-packages\PIL\PdfImagePlugin.py", line 41, in _save_all
_save(im, fp, filename, save_all=True)
File "C:\Users\felix\Anaconda3\lib\site-packages\PIL\PdfImagePlugin.py", line 156, in _save
raise ValueError(f"cannot save mode {im.mode}")
ValueError: cannot save mode RGBA
Has someone an idea what the problem is and how to fix it?
I thought I'm already converting each image to 'RGB'
. So, why do I get this error?