Skip to content
Related Articles

Related Articles

Python PIL | paste() and rotate() method
  • Last Updated : 18 Jun, 2019

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities.

PIL.Image.Image.paste() method is used to paste an image on another image. This is where the new() method comes in handy.

Syntax: PIL.Image.Image.paste(image_1, image_2, box=None, mask=None)
OR image_object.paste(image_2, box=None, mask=None)

Parameters:
image_1/image_object : It the image on which other image is to be pasted.
image_2: Source image or pixel value (integer or tuple).
box: An optional 4-tuple giving the region to paste into. If a 2-tuple is used instead, it’s treated as the upper left corner. If omitted or None, the source is pasted into the upper left corner.
If an image is given as the second argument and there is no third, the box defaults to (0, 0), and the second argument is interpreted as a mask image.

mask: An optional mask image.






# Importing Image module from PIL package
from PIL import Image
  
# creating a image object (main image)
im1 = Image.open(r"C:\Users\Admin\Pictures\network.PNG")
  
# creating a image object (image which is to be paste on main mage)
im2 = Image.open(r"C:\Users\Admin\Pictures\geeks.PNG")
  
# pasting im2 on im1
Image.Image.paste(im1, im2, (50, 125))
  
# to show specified image
im1.show()

Output:

PIL.Image.Image.rotate() method –

This method is used to rotate a given image to the given number of degrees counter clockwise around its centre.

Syntax:
new_object = PIL.Image.Image.rotate(image_object, angle, resample=0, expand=0)
OR
new_object = image_object.rotate(angle, resample=0, expand=0)

Either of the syntax can be used

Parameters:
image_object: It is the real image which is to be rotated.
angle: In degrees counter clockwise.
resample: An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbor), PIL.Image.BILINEAR (linear interpolation in a 2×2 environment), or PIL.Image.BICUBIC (cubic spline interpolation in a 4×4 environment). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.
expand: Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image.

Return Value: Returns a copy of rotated image.




# Importing Image module from PIL package
from PIL import Image
import PIL
  
# creating a image object (main image)
im1 = Image.open(r"C:\Users\Admin\Pictures\network.PNG")
  
# rotating a image 90 deg counter clockwise
im1 = im1.rotate(90, PIL.Image.NEAREST, expand = 1)
  
# to show specified image
im1.show()

Output:


Images used –

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :