Python PIL | composite() method

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.composite() method creates composite image by blending images using a transparency mask. Here, mask is another image which remains transparent when composite together.

Syntax: PIL.Image.composite(image1, image2, mask)

Parameters:
image1 – The first image.
image2 – The second image. Must have the same mode and size as the first image.
mask – A mask image. This image can have mode “1”, “L”, or “RGBA”, and must have the same size as the other two images.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Image module from PIL package
from PIL import Image
  
# creating a image1 object and converting it to mode 'L'
im1 = Image.open(r"C:\Users\sadow984\Desktop\c2.PNG").convert('L')
  
im1.show()

chevron_right


Showing image1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Image module from PIL package
from PIL import Image
  
# creating a image1 object and converting it to mode 'L'
im2 = Image.open(r"C:\Users\sadow984\Desktop\i2.PNG").convert('L')
im2.show()

chevron_right


Showing image2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Image module from PIL package
from PIL import Image
  
# creating a image1 object and converting it to mode 'L'
mask = Image.open(r"C:\Users\sadow984\Desktop\i3.PNG").convert('L')
mask.show()

chevron_right


Showing mask image:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Image module from PIL package
from PIL import Image
  
# creating a image1 object and converting it to mode 'L'
im1 = Image.open(r"C:\Users\sadow984\Desktop\c2.PNG").convert('L')
  
# creating a image2 object and converting it to mode 'L'
im2 = Image.open(r"C:\Users\sadow984\Desktop\i2.PNG").convert('L')
  
# creating a mask image object and converting it to mode 'L'
mask = Image.open(r"C:\Users\sadow984\Desktop\i3.PNG").convert('L')
  
# compositing all the thre images
im3 = Image.composite(im1, im2, mask)
  
# to show specified image 
im3.show()

chevron_right


Output: [the composite image]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.