How to flip an image horizontally or vertically in Pillow?

Sep 19, 2025

Leave a message

Flipping an image horizontally or vertically is a common image processing task that can be useful in various scenarios, such as creating mirror images, correcting orientation, or applying special effects. In this blog post, I'll share how to achieve this using the Pillow library. As a Pillow supplier, I've had the opportunity to work closely with this powerful library and understand its capabilities well.

Introduction to Pillow

Pillow is a popular Python library for image processing. It is a fork of the Python Imaging Library (PIL) and provides a wide range of functions for opening, manipulating, and saving different image file formats. With Pillow, you can perform basic operations like resizing, cropping, and rotating images, as well as more advanced tasks like applying filters and transformations.

Installation

Before we start, make sure you have Pillow installed. If you haven't installed it yet, you can use pip to install it:

pip install pillow

Flipping an Image Horizontally

Flipping an image horizontally means creating a mirror image of the original image along the vertical axis. In Pillow, this can be easily achieved using the transpose() method with the Image.FLIP_LEFT_RIGHT option.

Here is a simple Python code example:

from PIL import Image

# Open an image file
image = Image.open('your_image.jpg')

# Flip the image horizontally
flipped_horizontal = image.transpose(Image.FLIP_LEFT_RIGHT)

# Save the flipped image
flipped_horizontal.save('flipped_horizontal.jpg')

In this code, we first open an image file using the Image.open() method. Then, we use the transpose() method with the Image.FLIP_LEFT_RIGHT option to flip the image horizontally. Finally, we save the flipped image using the save() method.

Flipping an Image Vertically

Flipping an image vertically means creating a mirror image of the original image along the horizontal axis. Similar to horizontal flipping, we can use the transpose() method with the Image.FLIP_TOP_BOTTOM option to achieve this.

Here is the code example:

from PIL import Image

# Open an image file
image = Image.open('your_image.jpg')

# Flip the image vertically
flipped_vertical = image.transpose(Image.FLIP_TOP_BOTTOM)

# Save the flipped image
flipped_vertical.save('flipped_vertical.jpg')

In this code, we follow the same steps as before, but this time we use the Image.FLIP_TOP_BOTTOM option to flip the image vertically.

Practical Applications

Flipping images horizontally or vertically has many practical applications. For example, in graphic design, you might want to create a symmetrical pattern by flipping an image. In photography, you can use vertical flipping to correct the orientation of an upside - down photo. In computer vision, flipping images can be used as a data augmentation technique to increase the diversity of training data.

As a Pillow Supplier

As a Pillow supplier, we understand the importance of providing high - quality products and services. Our pillows are designed to meet the diverse needs of our customers. Whether you are looking for a Household Pillow for your daily use or a Memory Foam Pillow for better support and comfort, we have a wide range of options to choose from.

Our pillows are made from the finest materials and are crafted with precision to ensure maximum comfort and durability. We also offer customization services, so you can get a pillow that perfectly suits your preferences.

Performance and Optimization

When working with large images, flipping operations can be time - consuming. To optimize the performance, you can consider resizing the image before flipping it. Here is an example of resizing an image before flipping:

from PIL import Image

# Open an image file
image = Image.open('your_large_image.jpg')

# Resize the image
resized_image = image.resize((image.width // 2, image.height // 2))

# Flip the resized image horizontally
flipped_horizontal = resized_image.transpose(Image.FLIP_LEFT_RIGHT)

# Save the flipped image
flipped_horizontal.save('resized_flipped_horizontal.jpg')

In this code, we first resize the image to half of its original size using the resize() method. Then we perform the flipping operation on the resized image, which can significantly reduce the processing time.

Error Handling

When working with image files, it's important to handle potential errors. For example, if the image file does not exist or is corrupted, the Image.open() method will raise an exception. Here is an example of error handling:

from PIL import Image

try:
    # Open an image file
    image = Image.open('your_image.jpg')

    # Flip the image horizontally
    flipped_horizontal = image.transpose(Image.FLIP_LEFT_RIGHT)

    # Save the flipped image
    flipped_horizontal.save('flipped_horizontal.jpg')
except FileNotFoundError:
    print("The image file was not found.")
except Exception as e:
    print(f"An error occurred: {e}")

In this code, we use a try - except block to catch potential errors. If the file is not found, we print a specific error message. For other types of errors, we print a general error message.

Household PillowMemory Foam Pillow

Contact Us for Purchase

If you are interested in our pillows or have any questions about image processing using Pillow, please feel free to contact us. We are always ready to assist you with your purchase and provide professional advice. Whether you are a small business looking for bulk orders or an individual consumer, we can offer you the best solutions.

References

  • Pillow official documentation
  • Python official documentation

Send Inquiry