透明像素在 PIL 中被粘贴为黑色
Transparet pixels are being pasted as black in PIL
我粘贴了一张带有透明像素的图片,但在最终图片中它们是黑色的
img = Image.new('RGBA', (100, 100), "white")
arbol16 = Image.open("arbol16.png")
img.paste(arbol16,( 0, 0, 16, 16))
img.show()
arbol16.png
图片文件:
我认为您(可以理解)对 img.show()
显示的内容感到困惑 — 我认为这是错误的(实际上可能是一个错误)。在我的 Windows 系统上显示了一个临时的 .BMP 图像,看起来透明像素是黑色的。
但是如果你在末尾添加像img.save('arbol16_mod.png')
这样的一行,然后在某些图像文件查看程序中手动打开它来查看那个图像文件,例如Paint , Windows Photo Viewer, or Photoshop, 结果是正确的。
paste
方法没有考虑它粘贴的图像的 alpha;您需要添加一个 mask
参数来控制它。如果您要粘贴的图像是 RGBA
,您可以只传递两次。
arbol16 = arbol16.convert("RGBA")
img.paste(arbol16, (0, 0, 16, 16), arbol16)
我粘贴了一张带有透明像素的图片,但在最终图片中它们是黑色的
img = Image.new('RGBA', (100, 100), "white")
arbol16 = Image.open("arbol16.png")
img.paste(arbol16,( 0, 0, 16, 16))
img.show()
arbol16.png
图片文件:
我认为您(可以理解)对 img.show()
显示的内容感到困惑 — 我认为这是错误的(实际上可能是一个错误)。在我的 Windows 系统上显示了一个临时的 .BMP 图像,看起来透明像素是黑色的。
但是如果你在末尾添加像img.save('arbol16_mod.png')
这样的一行,然后在某些图像文件查看程序中手动打开它来查看那个图像文件,例如Paint , Windows Photo Viewer, or Photoshop, 结果是正确的。
paste
方法没有考虑它粘贴的图像的 alpha;您需要添加一个 mask
参数来控制它。如果您要粘贴的图像是 RGBA
,您可以只传递两次。
arbol16 = arbol16.convert("RGBA")
img.paste(arbol16, (0, 0, 16, 16), arbol16)