我正在尝试使用 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/