python - PIL 将透明 PNG 加载为 RGB?

标签 python python-imaging-library

我正在尝试使用 PIL 来合成一些航空图像,但遇到了一些麻烦。我使用PIL加载this image用这一点代码:

composite = Image.new('RGBA', (256, 256))
url = 'http://...'
resp = requests.get(url)
content = StringIO(resp.content)
image = Image.open(content)
composite.paste(image, (0, 0), image)

当我调用 composite.paste() 时,PIL 给出错误“ValueError:坏透明度蒙版”。当我打印 image.mode 时,果然它只是 RGB 而不是预期的 RGBA (其中 paste()需要)。

我下载的 PNG 上的 Alpha channel 去哪里了?

最佳答案

下面的代码对我有用:

from PIL import Image
import requests
import StringIO

url = "https://gis.apfo.usda.gov/arcgis/rest/services/NAIP/Tennessee_2016_60cm/ImageServer/exportImage?bbox=-87.1875,34.3071438563,-84.375,36.5978891331&bboxSR=4326&size=256,256&imageSR=102113&transparent=true&format=png&f=image"
resp = requests.get(url)
content = StringIO.StringIO(resp.content)
image = Image.open(content)
image = image.convert('RGBA')
composite = Image.new("RGBA", image.size, (255,255,255,0))
composite.paste(image )

关于python - PIL 将透明 PNG 加载为 RGB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42209327/

相关文章:

python - Mongoengine 弃用警告 : insert is deprecated

python - 检查文本是否以空格开头

Python PIL Crop 不裁剪图像

python - PIL IO错误: decoder zip not available after built with ZLIB support

python - 为 click.options() 创建包装装饰器

python - 如果子项中没有某些内容,我如何附加父项中的内容?

python - 将 PIL 图像转换为 numpy 数组有时不起作用

python - 当 Canvas 放在框架内时,Tkinter 不会在 Canvas 上显示图片

python - 使用 readchar 包在 python 中读取字符

python - 让 Django 识别 PIL JPEG 支持