How to Flip an Image Vertically or Horizontally in Python
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
Image manipulation can be done using the PIL (Pillow) library.
- Install Pillow:
- Use pip to install the library.
pip install Pillow - Flipping an Image:
- Use the
transpose()method with the appropriate method.
- Use the
- 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")
- Explanation:
FLIP_LEFT_RIGHTflips the image horizontally.FLIP_TOP_BOTTOMflips the image vertically.
solveurit24@gmail.com Changed status to publish February 16, 2025