PIL 将具有透明度的 PNG 或 GIF 转换为不带透明度的 JPG
我正在使用 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", infile
两个脚本都是独立工作的,但是当我将它们组合起来时,我得到一个 ValueError: Bad Transparency Mask。
Traceback (most recent call last):
File "pilhello.py", line 17, in <module>
background.paste(im, im)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1101, in paste
self.im.paste(im, box, mask.im)
ValueError: bad transparency mask
我怀疑如果我要保存不透明的PNG,我可以打开该新文件,并将其重新保存为JPG,并删除写入磁盘的PNG,但我希望有一个优雅的解决方案我还没有找到。
I'm prototyping an image processor in Python 2.7 using PIL1.1.7 and I would like all images to end up in JPG. Input file types will include tiff,gif,png both with transparency and without. I've been trying to combine two scripts that I found that 1. convert other file types to JPG and 2. removing transparency by creating a blank white image and pasting the original image over the white background. My searches are being spammed with people seeking to generate or preserve transparency rather than the opposite.
I'm currently working with this:
#!/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", infile
Both scripts work in isolation, but as I have combined them I get a ValueError: Bad Transparency Mask.
Traceback (most recent call last):
File "pilhello.py", line 17, in <module>
background.paste(im, im)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1101, in paste
self.im.paste(im, box, mask.im)
ValueError: bad transparency mask
I suspect that if I were to save a PNG without transparency I could then open that new file, and re-save it as a JPG, and delete the PNG that was written to disk, but I'm hoping that there is an elegant solution that I haven't found yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将背景设置为 RGB,而不是 RGBA。当然,还要删除稍后将背景转换为 RGB 的操作,因为它已经处于该模式了。这对我创建的测试图像有用:
Make your background RGB, not RGBA. And remove the later conversion of the background to RGB, of course, since it's already in that mode. This worked for me with a test image I created:
关键是使蒙版(用于粘贴)成为图像本身。
这应该适用于那些具有“软边缘”的图像(其中 alpha 透明度设置为不为 0 或 255)
The key is to make the mask (for the paste) the image itself.
This should work on those images that have "soft edges" (where the alpha transparency is set to not be 0 or 255)
以下内容适用于我这张图片
The following works for me on this image
要将 png 转换为 jpg,请运行
png2jpg("try.png", (255,255,255))
结果:
To convert png to jpg run
png2jpg("try.png", (255,255,255))
Result: