Skip to content
Related Articles

Related Articles

Python Pillow – Using Image Module

View Discussion
Improve Article
Save Article
Like Article
  • 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 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)

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 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)

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 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)

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 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)

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!