如何使用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)