Skip to content
Related Articles

Related Articles

Improve Article
Python Pillow – Using Image Module
  • Last Updated : 21 Apr, 2021

In this article, we will see how to work with Image Module of PIL in Python. First, let’s see how to install the PIL. 


Linux: On the Linux terminal type the following:

pip install Pillow

Installing pip via terminal:

sudo apt-get update
sudo apt-get install python-pip

Working with Image Module

Here, we will go through some methods and properties provided by the image module which are as follows:

  • Open Image,
  • Save Image,
  • Size of Image,
  • Rotate Image,
  • Crop Image,
  • Resize Image and many more.

 Let’s discuss this one by one with the help of some examples.

1. Open An Image:

To open the image using PIL, we are using the open() method.

Syntax:, mode=’r’, formats=None)


# importing Image from PIL
from PIL import Image
# open an image
img ='gfg.png')


2. Retrieve size of the image:

To retrieve the size of the image we will use the property provided by the Image object i.e; Image.size property.

Syntax: Image.size


from PIL import Image
with"gfg.png") as image:
    width, height = image.size


(200, 200)

3. Save changes in the image:

To save the image, we are using method.

Syntax:, format=None, **params)


  • fp – A filename (string), pathlib.Path object or file object.
  • format – Optional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.
  • options – Extra parameters to the image writer.

Returns: None


from PIL import Image
img ="gfg.png")"logo.jpg")


4. Rotating an Image:

The image rotation needs an angle as a parameter to get the image rotated.

Syntax: Image.rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None)


  • angle – In degrees counterclockwise.
  • resample – An optional resampling filter.
  • expand – Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image.
  • center – Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.
  • translate – An optional post-rotate translation (a 2-tuple).
  • fillcolor – An optional color for area outside the rotated image.


from PIL import Image
img ="gfg.png")
rot_img = img.rotate(180)"rotated_gfg.png")


Original Image

Rotated Image

5. Cropping an Image: 

The Image.crop(box) takes a 4-tuple (left, upper, right, lower) pixel coordinate, and returns a rectangular region from the used image.

Syntax: PIL.Image.crop(box = None)


  • box – a 4-tuple defining the left, upper, right, and lower pixel coordinate.

Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).

Return: An Image object.


from PIL import Image
#  open image and get size
img ="gfg.jpg")
width, height = img.size
# cropped image using corrdinates
area = (0, 0, width/2, height/2)
crop_img = img.crop(area)"cropped_image.jpg")


Cropped Image

6. Resizing an Image: 

The Image.resize(size) is used to resize. Here size is provided as a 2-tuple width and height.

Syntax: Image.resize(size, resample=0)


  • size – The requested size in pixels, as a 2-tuple: (width, height).
  • resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.

Returns type: An Image object.


from PIL import Image
img ="gfg.png")
width, height = img.size
#resizing the image  
img = img.resize((width//2, height//2))"resized_picture.png")


Resized Image

7. Pasting an image on another image: 

The second argument can be a 2-tuple (specifying the top left corner), or a 4-tuple (left, upper, right, lower) – in this case, the size of the pasted image must match the size of this box region or None which is equivalent to (0, 0).

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


             image_object.paste(image_2, box=None, mask=None)


  • 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.
  • mask: An optional mask image.

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.


from PIL import Image
img1 ="gfg.jpg")
#pasting img2 on img1
img2 ="gfg.png")
img1.paste(img2, (50, 50))"pasted_picture.jpg")


8. Transposing an Image: 

This feature gives us the mirror image of an image

Syntax: Transpose image (flip or rotate in 90 degree steps)


  • method – One of PIL.Image.FLIP_LEFT_RIGHT, PIL.Image.FLIP_TOP_BOTTOM, PIL.Image.ROTATE_90, PIL.Image.ROTATE_180, PIL.Image.ROTATE_270 or PIL.Image.TRANSPOSE.

Returns type: An Image object.


from PIL import Image
img ="gfg.png")      
#flipping the image by 180 degree horizontally
transposed_img = img.transpose(Image.FLIP_LEFT_RIGHT)



Transposed Image

9. Creating a thumbnail: 

This method creates a thumbnail of the image that is opened. It does not return a new image object, it makes in-place modifications to the currently opened image object itself. If you do not want to change the original image object, create a copy and then apply this method. This method also evaluates the appropriate to maintain the aspect ratio of the image according to the size passed.

Syntax: Image.thumbnail(size, resample=3)


  • size – Requested size.
  • resample – Optional resampling filter.

Returns Type: An Image object.


from PIL import Image
img ="gfg.png"
img.thumbnail((200, 200))"thumb.png")


Thumbnail of 200 x 200 pixels

 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 :