使用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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章