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.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  • 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.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Example code

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

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

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.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Example code

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

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

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.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Common query

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved