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

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

 

im.paste(image, box, mask)

Same as above, but updates only the regions indicated by the mask. You can use either "1", "L" or "RGBA" images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate values can be used for transparency effects.

Note that if you paste an "RGBA" image, the alpha band is ignored. You can work around this by using the same image as both source image and 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()

 

  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值