Python | Copy and Paste Images onto other Image using Pillow

In this article, we will learn how to copy an image over another image using pillow library. We will use image module from pillow and copy() and paste() methods to achieve this task.

We will need to create copies of both images so that it does not affect the original image with the help of copy() method and then paste the image on the other image with the help of paste() method.

Input images –



Image1:

Image2:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import image module from pillow
from PIL import Image
  
# open the image
Image1 = Image.open('D:\cat.jpg')
  
# make a copy the image so that the 
# original image does not get affected
Image1copy = Image1.copy()
Image2 = Image.open('D:\core.jpg')
Image2copy = Image2.copy()
  
# paste image giving dimensions
Image1copy.paste(Image2copy, (0, 0))
  
# save the image 
Image1copy.save('D:\pasted2.png')

chevron_right


Output:

Example 2: Changing paprameters to place the Image2 on face of the cat in the Image1.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import image module from pillow
from PIL import Image
  
# open the image
Image1 = Image.open('D:\cat.jpg')
  
# make a copy the image so that 
# the original image does not get affected
Image1copy = Image1.copy()
Image2 = Image.open('D:\core.jpg')
Image2copy = Image2.copy()
  
# paste image giving dimensions
Image1copy.paste(Image2copy, (70, 150))
  
# save the image 
Image1copy.save('D:\pasted2.png')

chevron_right


Output:



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.



Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.


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