How to flip an image in Python pillow

Pillow is a fork for the Python Imaging Library, PIL, that provides various image-processing features to manipulate images and apply different filters. It is a powerful library that is easy to integrate with other Python libraries and frameworks. By using the pillow library, we can perform various operations on an image as follows:

In this Answer, we shall apply vertical and horizontal flips to an image and observe how the image changes.

Required imports

We will use the PIL library to manipulate the image and then use matplotlib to display the results once the code is executed successfully.

import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
  • matplotlib: To create visualizations and plots in the Python library.

  • PIL: Computer vision library to apply operations on images.

    • Image: To manipulate the images.

  • requests: To send the requests over the HTTP server to the website.

  • BytesIO: To handle the binary data as an in-memory system.

Apply verticle flip

A verticle flip refers to reversing the order of the image's row. The top-row pixels of the image are swapped with the bottom-row pixels of the images, and consequently, the image pixels are vertically swapped. We use the Image.FLIP_TOP_BOTTOM method to vertically flip the image.

Expected output

We flip the image vertically along the y-axis to get an image that is an upside-down mirror reflection of the original image.

Vertically flipped image.
Vertically flipped image.

Example code

In this example, we fetch an online image through its link address and flip it vertically using transpose().

import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO

image_url = "https://images.pexels.com/photos/7006256/pexels-photo-7006256.jpeg?auto=compress&cs=tinysrgb&w=300"

# Fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)

# Apply vertical flip
vertical_flip_image = original_image.transpose(Image.FLIP_TOP_BOTTOM)

plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)

plt.subplot(1, 2, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')

plt.subplot(1, 2, 2) 
plt.imshow(vertical_flip_image)
plt.axis('off')
plt.title('Vertically Flipped Image')

plt.show()
Flip the image vertically.

Code explanation

  • Lines 1–4: Import the required libraries and modules.

  • Line 6: Store the link of the image that is to be used on image_url. We can also use a local image file and give its exact name.

  • Lines 9–11: Use request to fetch the image from the server and open it using the Image.open() method and pass the converted image to it.

  • Line 14: Use the transpose() method of the Image class and apply FLIP_TOP_BOTTOM to swap image pixels vertically.

  • Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.

  • Lines 19–22: Display the image using imshow() and pass the original image to it, turn off the x-axis labels, and specify the title in the title().

  • Lines 24–27: Display the image using imshow() and pass the flipped image to it, turn off the x-axis labels, and specify the title in the title().

  • Line 29: Show the resultant plot.

Apply horizontal flip

A horizontal flip refers to reversing the order of the image's column order. The left-column pixels of the image are swapped with the right-column pixels of the image, and consequently, the image pixels are horizontally swapped. We use the Image.FLIP_LEFT_RIGHT method to horizontally flip the image.

Expected output

We flip the image horizontally along the x-axis to get an image that is a left-right mirror reflection of the original image.

Horizontally flipped image.
Horizontally flipped image.

Example code

In this example, we fetch an online image through its link address and flip it horizontally using transpose().

import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO

image_url = "https://images.pexels.com/photos/7006256/pexels-photo-7006256.jpeg?auto=compress&cs=tinysrgb&w=300"

# Fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)

# Apply horizontal flip
horizontal_flip_image = original_image.transpose(Image.FLIP_LEFT_RIGHT)

plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)

plt.subplot(1, 2, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')

plt.subplot(1, 2, 2) 
plt.imshow(horizontal_flip_image)
plt.axis('off')
plt.title('Horizontally Flipped Image')

plt.show()
Flip the image horizontally.

Code explanation

  • Lines 1–4: Import the required libraries and modules.

  • Line 6: Store the link of the image that is to be used on image_url. We can also use a local image file and give its exact name.

  • Lines 9–11: Use request to fetch the image from the server and open it using the Image.open() method and pass the converted image to it.

  • Line 14: Use the transpose() method of Image class and apply FLIP_LEFT_RIGHT to swap image pixels horizontally.

  • Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.

  • Lines 19–22: Display the image using imshow() and pass the original image to it, turn off the x-axis labels, and specify the title in the title().

  • Lines 24–27: Display the image using imshow() and pass the flipped image to it, turn off the x-axis labels, and specify the title in the title().

  • Line 29: Show the resultant plot.

Real-life application

There are a lot of real-life scenarios where there is a need to flip an image. Let's take a look at a few of the scenarios where image flipping is useful.

Real life applications of flipped image.
Real life applications of flipped image.

Common query

Question

Why do we use transpose() to flip the images?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved