在Python中叠加两个相同大小的图像 [英] Overlay two same sized images in Python
本文介绍了在Python中叠加两个相同大小的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张尺寸完全相同的图像,我要做的就是拍摄一张,使其具有50%的透明度,然后将其直接放在另一张上面,就像这样:
I've got two images that are exactly the same dimensions, all I'm trying to do is take one, make it 50% transparent and place it directly on top of the other, like so:
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%透明的覆盖层(就这样一半了!).
But all I get is the 50% transparent overlay (so half way there!).
推荐答案
尝试使用 blend()而不是paste()-似乎paste()只是将您要粘贴的图像替换为原始图像.
Try using blend() instead of paste() - it seems paste() just replaces the original image with what you're pasting in.
import Image
background = Image.open("bg.png")
overlay = Image.open("ol.jpg")
background = background.convert("RGBA")
overlay = overlay.convert("RGBA")
new_img = Image.blend(background, overlay, 0.5)
new_img.save("new.png","PNG")
这篇关于在Python中叠加两个相同大小的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文