How to Flip an Image Vertically or Horizontally in Python

77 views

How to Flip an Image Vertically or Horizontally in Python

How to Flip an Image Vertically or Horizontally in Python

I need to flip an image vertically or horizontally using Python. How can I achieve this without external libraries?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

Image manipulation can be done using the PIL (Pillow) library.

  1. Install Pillow:
    • Use pip to install the library.
    pip install Pillow
    
  2. Flipping an Image:
    • Use the transpose() method with the appropriate method.
  3. Code Example:

    from PIL import Image
    
    # Open an image
    img = Image.open("image.jpg")
    
    # Flip horizontally
    img_horizontal = img.transpose(Image.FLIP_LEFT_RIGHT)
    img_horizontal.save("horizontal_flip.jpg")
    
    # Flip vertically
    img_vertical = img.transpose(Image.FLIP_TOP_BOTTOM)
    img_vertical.save("vertical_flip.jpg")
    


  4. Explanation:
    • FLIP_LEFT_RIGHT flips the image horizontally.
    • FLIP_TOP_BOTTOM flips the image vertically.
solveurit24@gmail.com Changed status to publish February 16, 2025
0