python处理RGBA格式的透明图片的粘贴

原文:https://blog.csdn.net/robinzhou/article/details/6960345

当需要将一张有透明部分的图片粘贴到一张底片上时,如果用Python处理,可能会用到PIL,但是PIL中 有说明,在粘贴RGBA模式的图片是,alpha通道不会被帖上,也就是不会有透明的效果,当然也给出了解决方法,就是粘贴的时候,将RGBA的的alpha通道提取出来做为mask传入。

im.paste(image, box, mask)

代码如下:

#读取底片

imp = Image.open('20111110_170002.jpg')

#读取要粘贴的图片 RGBA模式

imq = Image.open('attachment.png')

#分离通道

 r,g,b,a = imq.split()

#粘贴

imp.paste(imq,(100, 100, 171, 172),mask = a)

显示:

imp.show()

out_display.paste(new_img, (int(picture_display[0])+10, int(picture_display[1])+10), mask = a)

将分离出的通道贴到 out_display 图像中并保存

plt.imshow(out_display)
plt.savefig('F:/hackathon/pic/display/image_output.png', dpi=500, bbox_inches='tight')

猜你喜欢

转载自www.cnblogs.com/Allen-rg/p/12097340.html