相关疑难解决方法(0)

使用PIL将RGBA PNG转换为RGB

我正在使用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)

python png jpeg python-imaging-library rgba

85
推荐指数
3
解决办法
10万
查看次数

如何使用PIL获取PNG图像的alpha值?

如何使用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软件包.

python png image transparent python-imaging-library

21
推荐指数
2
解决办法
3万
查看次数

标签 统计

png ×2

python ×2

python-imaging-library ×2

image ×1

jpeg ×1

rgba ×1

transparent ×1