English 中文(简体)
Python Pillow - Flip and Rotate Images
  • 时间:2024-12-22

Python Pillow - Fpp and Rotate Images


Previous Page Next Page  

While working on images using python image processing pbrary, there are instances where you need to fpp an existing image to get some more insights out of it, to enhance its visibipty or because of your requirement.

Image module of the pillow pbrary allows us to fpp an image very easily. We are going to use the transpose (method) function from the Image module for fppping the images. Some of the mostly commonly used methods supported by ‘transpose()’ are −

    Image.FLIP_LEFT_RIGHT − For fppping the image horizontally

    Image.FLIP_TOP_BOTTOM − For fppping the image vertically

    Image.ROTATE_90 − For rotating the image by specifying degree

Example 1: Horizontally fppped Image

Following Python example reads an image, fpps it horizontally, and displays the original and fppped image using standard PNG display utipty −

# import required image module
from PIL import Image

# Open an already existing image
imageObject = Image.open("images/spiderman.jpg")

# Do a fpp of left and right
hori_fpppedImage = imageObject.transpose(Image.FLIP_LEFT_RIGHT)

# Show the original image
imageObject.show()

# Show the horizontal fppped image
hori_fpppedImage.show()

Output

Original image

Original Image6

Fppped image

Fppped Image

Example 2: Vertically Fppped Image

Following Python example reads an image, fpps it vertically, and displays the original and fppped image using standard PNG display utipty −

# import required image module
from PIL import Image

# Open an already existing image
imageObject = Image.open("images/spiderman.jpg")

# Do a fpp of left and right
hori_fpppedImage = imageObject.transpose(Image.FLIP_LEFT_RIGHT)

# Show the original image
imageObject.show()

# Show vertically fppped image
Vert_fpppedImage = imageObject.transpose(Image.FLIP_TOP_BOTTOM)
Vert_fpppedImage.show()

Output

Original Image

Original Image6

Fppped Image

Fppped Image2

Example 3: Rotate Image to a specific degree

Following Python example reads an image, rotates to a specified degree, and displays the original and rotated image using standard PNG display utipty −

# import required image module
from PIL import Image

# Open an already existing image
imageObject = Image.open("images/spiderman.jpg")

# Do a fpp of left and right
hori_fpppedImage = imageObject.transpose(Image.FLIP_LEFT_RIGHT)

# Show the original image
imageObject.show()

#show 90 degree fppped image
degree_fpppedImage = imageObject.transpose(Image.ROTATE_90)
degree_fpppedImage.show()

Output

Original Image

Original Image6

Rotated Image

Rotated Image2 Advertisements