我有两张尺寸完全相同的图像,我要做的就是拍摄一张,使其透明 50%,然后将其直接放在另一张的顶部,如下所示:
import Image
background = Image.open("bg.png")
overlay = Image.open("over.png")
background = background.convert("RGBA")
overlay = overlay.convert("RGBA")
background_pixels = background.load()
overlay_pixels = overlay.load()
for y in xrange(overlay.size[1]):
for x in xrange(overlay.size[0]):
background_pixels[x,y] = (background_pixels[x,y][0], background_pixels[x,y][1], background_pixels[x,y][2], 255)
for y in xrange(overlay.size[1]):
for x in xrange(overlay.size[0]):
overlay_pixels[x,y] = (overlay_pixels[x,y][0], overlay_pixels[x,y][1], overlay_pixels[x,y][2], 128)
background.paste(overlay)
background.save("new.png","PNG")
但我得到的只是 50% 的透明覆盖层(所以到了一半!)。