为什么在使用 PIL 时会出现 "cannot save mode RGBA" 错误?
Why do I get "cannot save mode RGBA" error when using PIL?
我正在尝试将图像转换为 PDF。
我有这个代码:
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
我有时会收到此错误:
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
有人知道问题出在哪里以及如何解决吗?
我以为我已经将每个图像转换为 'RGB'
。那么,为什么会出现此错误?
.convert('RGB')
;它意味着在 RGB
图像和基于调色板的图像之间进行转换(它的主要操作是 P
调色板,L
单通道(灰度)和 RGB
单独通道).
而是 you have to be explicit 关于进行转换以删除 alpha 通道。来自链接的答案:
from PIL import Image
png = Image.open(object.logo.path)
png.load() # required for png.split()
background = Image.new("RGB", png.size, (255, 255, 255))
background.paste(png, mask=png.split()[3]) # 3 is the alpha channel
然后您可以在图像列表中使用 background
并将其保存为 PDF。您现在还明确地使这些图像具有白色背景。
我正在尝试将图像转换为 PDF。 我有这个代码:
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
我有时会收到此错误:
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
有人知道问题出在哪里以及如何解决吗?
我以为我已经将每个图像转换为 'RGB'
。那么,为什么会出现此错误?
.convert('RGB')
RGB
图像和基于调色板的图像之间进行转换(它的主要操作是 P
调色板,L
单通道(灰度)和 RGB
单独通道).
而是 you have to be explicit 关于进行转换以删除 alpha 通道。来自链接的答案:
from PIL import Image
png = Image.open(object.logo.path)
png.load() # required for png.split()
background = Image.new("RGB", png.size, (255, 255, 255))
background.paste(png, mask=png.split()[3]) # 3 is the alpha channel
然后您可以在图像列表中使用 background
并将其保存为 PDF。您现在还明确地使这些图像具有白色背景。