Python Pillow - Using Image Module
Last Updated :
19 Nov, 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.
Installation:
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: PIL.Image.open(fp, mode='r', formats=None)
Python3
# importing Image from PIL
from PIL import Image
# open an image
img = Image.open('gfg.png')
Output:

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
Python3
from PIL import Image
with Image.open("gfg.png") as image:
width, height = image.size
print((width,height))
Output:
(200, 200)
3. Save changes in the image:
To save the image, we are using Image.save() method.
Syntax: Image.save(fp, format=None, **params)
Parameters:
- 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
Python3
from PIL import Image
img = Image.open("gfg.png")
img.save("logo.jpg")
Output:

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)
Parameters:
- 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.
Python3
from PIL import Image
img = Image.open("gfg.png")
rot_img = img.rotate(180)
rot_img.save("rotated_gfg.png")
Output:
Original Image
Rotated Image5. 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)
Parameters:
- 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.
Python3
from PIL import Image
# open image and get size
img = Image.open("gfg.jpg")
width, height = img.size
# cropped image using coordinates
area = (0, 0, width/2, height/2)
crop_img = img.crop(area)
crop_img.save("cropped_image.jpg")
Output:
Cropped Image6. 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)
Parameters:
- 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.
Python3
from PIL import Image
img = Image.open("gfg.png")
width, height = img.size
#resizing the image
img = img.resize((width//2, height//2))
img.save("resized_picture.png")
Output:
Resized Image7. 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)
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.
- 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.
Python3
from PIL import Image
img1 = Image.open("gfg.jpg")
#pasting img2 on img1
img2 = Image.open("gfg.png")
img1.paste(img2, (50, 50))
img1.save("pasted_picture.jpg")
Output:

8. Transposing an Image:
This feature gives us the mirror image of an image
Syntax: Transpose image (flip or rotate in 90 degree steps)
Parameters:
- 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.
Python3
from PIL import Image
img = Image.open("gfg.png")
#flipping the image by 180 degree horizontally
transposed_img = img.transpose(Image.FLIP_LEFT_RIGHT)
transposed_img.save("transposed.png")
Output:
Original
Transposed Image9. 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)
Parameters:
- size – Requested size.
- resample – Optional resampling filter.
Returns Type: An Image object.
Python3
from PIL import Image
img = Image.open("gfg.png")
img.thumbnail((200, 200))
img.save("thumb.png")
Output:
Thumbnail of 200 x 200 pixels
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read