如何使用PIL检测PNG图像是否具有透明的alpha通道?
img = Image.open('example.png', 'r')
has_alpha = img.mode == 'RGBA'
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我们知道PNG图像是否具有alpha通道,但是如何获得alpha值?
我没有在PIL网站上描述的img.info字典中找到"透明度"键
我正在使用Ubuntu和zlib1g,已经安装了zlibc软件包.
我正在使用PIL1.1.7在Python 2.7中对图像处理器进行原型设计,我希望所有图像都以JPG结尾.输入文件类型将包括透明和没有的tiff,gif,png.我一直在尝试组合两个脚本,我发现1.将其他文件类型转换为JPG和2.通过创建空白图像并将原始图像粘贴到白色背景上来消除透明度.我的搜索是垃圾邮件,人们试图产生或保持透明度,而不是相反.
我正在使用这个:
#!/usr/bin/python
import os, glob
import Image
images = glob.glob("*.png")+glob.glob("*.gif")
for infile in images:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
#try:
im = Image.open(infile)
# Create a new image with a solid color
background = Image.new('RGBA', im.size, (255, 255, 255))
# Paste the image on top of the background
background.paste(im, im)
#I suspect that the problem is the line below
im = background.convert('RGB').convert('P', palette=Image.ADAPTIVE)
im.save(outfile)
#except IOError:
# print "cannot convert", …Run Code Online (Sandbox Code Playgroud) 我有两个图像,都有alpha通道.我想将一个图像放在另一个图像上,从而生成带有alpha通道的新图像,就像它们在图层中呈现一样.我想用Python Imaging Library做到这一点,但是其他系统中的建议会很棒,即使原始数学也是一个好处; 我可以使用NumPy.
我在这个问题上搜索了很多,但却找不到我需要的东西.我会解释我的问题:
在我的网站上,用户可以上传图像.我需要调整此图像的大小并在上传之前将其转换为JPEG .
我在Views.py中看到了一些使用方法的解决方案,但我想通过覆盖Models.py中的save()方法来实现.问题是,我发现在save方法中调整图像大小的所有解决方案都是在第一次保存(调用超级函数)之后执行的,这意味着它会使用带宽(如果我使用CDN)什么都没有(我是对的吗?点?).
谢谢您的帮助
如何用指定的背景颜色替换任何图像(png,jpg,rgb,rbga)的alpha通道?它还必须适用于没有Alpha通道的图像.
我正在尝试将图像转换为 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
Run Code Online (Sandbox Code Playgroud)
有时我会收到此错误:
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 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)
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud)