我有两张完全相同尺寸的图片,我想做的只是将其中一张图片透明度降低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%透明的叠加层(已经完成了一半!)。
from PIL import Image
适用于Python 3.x - Trect