我正在使用PIL将使用Django上传的透明PNG图像转换为JPG文件.输出看起来很糟糕.

Image.open(object.logo.path).save('/tmp/output.jpg', 'JPEG')
Run Code Online (Sandbox Code Playgroud)
要么
Image.open(object.logo.path).convert('RGB').save('/tmp/output.png')
Run Code Online (Sandbox Code Playgroud)
两种方式,生成的图像如下所示:

有没有办法来解决这个问题?我想要有透明背景的白色背景.
感谢很棒的答案,我想出了以下函数集合:
import Image
import numpy as np
def alpha_to_color(image, color=(255, 255, 255)):
"""Set all fully transparent pixels of an RGBA image to the specified color.
This is a very simple solution that might leave over some ugly edges, due
to semi-transparent areas. You should use alpha_composite_with color instead.
Source: http://stackoverflow.com/a/9166671/284318
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
""" …Run Code Online (Sandbox Code Playgroud) 如何使用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软件包.