Monday, December 8, 2025
HomeLanguagesHow to flip an image horizontally or vertically in Python?

How to flip an image horizontally or vertically in Python?

Prerequisites: PIL

Given an image the task here is to generate a Python script to flip an image horizontally and vertically. Here the module used for the task is PIL and transpose() function of this module. 

Syntax:

transpose(degree)

Keywords FLIP_TOP_BOTTOM and FLIP_LEFT_RIGHT will be passed to transpose method to flip it.

  • FLIP_TOP_BOTTOM – returns an original image flipped Vertically
  • FLIP_LEFT_RIGHT- returns an original image flipped Horizontally

Approach

  • Import module
  • Open original image
  • Transform the image as required
  • Save the new transformed image.

Image in use:

Example: Flipping image vertically

Python3




# importing PIL Module
from PIL import Image
 
# open the original image
original_img = Image.open("original.png")
 
# Flip the original image vertically
vertical_img = original_img.transpose(method=Image.FLIP_TOP_BOTTOM)
vertical_img.save("vertical.png")
 
# close all our files object
original_img.close()
vertical_img.close()


Output:

Example : Flip image horizontally

Python3




# importing PIL Module
from PIL import Image
 
# open the original image
original_img = Image.open("original.png")
 
 
# Flip the original image horizontally
horz_img = original_img.transpose(method=Image.FLIP_LEFT_RIGHT)
horz_img.save("horizontal.png")
 
# close all our files object
original_img.close()
horz_img.close()


Output:

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32429 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11945 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12015 POSTS0 COMMENTS
Shaida Kate Naidoo
6934 POSTS0 COMMENTS
Ted Musemwa
7189 POSTS0 COMMENTS
Thapelo Manthata
6883 POSTS0 COMMENTS
Umr Jansen
6869 POSTS0 COMMENTS