How to merge a transparent png image with another image using PIL

I have a transparent png image "foo.png" and I opened another image with

im = Image.open("foo2.png"); 

Now I need to merge foo.png with foo2.png.

(foo.png contains some text, and I want to print this text on foo2.png)

+125
python image image-processing python-imaging-library
Mar 16 '11 at 11:33
source share
6 answers
 import Image background = Image.open("test1.png") foreground = Image.open("test2.png") background.paste(foreground, (0, 0), foreground) background.show() 

The first parameter .paste() is the image to insert. Secondly, these are the coordinates, and secret sauce is the third parameter. He points the mask which will be used to insert the image. If you are transmitting an image with transparency, then the alpha channel is used as a mask.

Check out the docs .

+218
Mar 16 2018-11-11T00:
source share

Image.paste does not work properly if the background image also contains transparency. You need to use real Alpha Compositing .

Pillow 2.0 contains an alpha_composite function that does this.

 background = Image.open("test1.png") foreground = Image.open("test2.png") Image.alpha_composite(background, foreground).save("test3.png") 

EDIT: Both images must be RGBA type. So you need to call convert('RGBA') if they are scaled, etc. If the background does not have an alpha channel, you can use the usual paste method (which should be faster).

+53
Apr 10 '13 at 7:45
source share

As olt already pointed out, Image.paste does not work correctly when the source and destination contain alpha.

Consider the following scenario:

Two test images, both contain alpha:

enter image description here enter image description here

 layer1 = Image.open("layer1.png") layer2 = Image.open("layer2.png") 

Map an image using Image.paste as follows:

 final1 = Image.new("RGBA", layer1.size) final1.paste(layer1, (0,0), layer1) final1.paste(layer2, (0,0), layer2) 

creates the following image (the alpha part of the superimposed red pixels is completely taken from the second layer. Pixels do not mix correctly):

enter image description here

Map an image using Image.alpha_composite as follows:

 final2 = Image.new("RGBA", layer1.size) final2 = Image.alpha_composite(final2, layer1) final2 = Image.alpha_composite(final2, layer2) 

displays the following (correct) image:

enter image description here

+37
Apr 20 '17 at 8:50
source share

You can also use blending:

 im1 = Image.open("im1.png") im2 = Image.open("im2.png") blended = Image.blend(im1, im2, alpha=0.5) blended.save("blended.png") 
+7
Sep 08 '17 at 11:19 on
source share

I had a similar question and hardly found the answer. The following function allows you to insert an image with a transparency parameter on top of another image with a specific offset.

 import Image def trans_paste(fg_img,bg_img,alpha=1.0,box=(0,0)): fg_img_trans = Image.new("RGBA",fg_img.size) fg_img_trans = Image.blend(fg_img_trans,fg_img,alpha) bg_img.paste(fg_img_trans,box,fg_img_trans) return bg_img bg_img = Image.open("bg.png") fg_img = Image.open("fg.png") p = trans_paste(fg_img,bg_img,.7,(250,100)) p.show() 
0
Dec 07 '18 at 4:25
source share
 def trans_paste(bg_img,fg_img,box=(0,0)): fg_img_trans = Image.new("RGBA",bg_img.size) fg_img_trans.paste(fg_img,box,mask=fg_img) new_img = Image.alpha_composite(bg_img,fg_img_trans) return new_img 
0
Apr 28 '19 at 7:16
source share



All Articles