
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - Flip and Rotate Images
Flipping and rotating images are basic image processing tasks often used to enhance visibility, correct orientation, or achieve specific effects. The Python Pillow library provides simple methods to perform these operations on images.
Flipping images
Flipping an image is an image processing operation that involves reversing the orientation of the image along a specified axis. This operation can be performed either horizontally i.e. left to right or vertically i.e. top to bottom. In digital image processing flipping is often used for various purposes such as mirroring, correcting for camera orientation or achieving artistic effects.
In the context of image flipping there are two primary types, one is horizontal flip and other is vertical flip.
Horizontal Flip (Left to Right) − In a horizontal flip each row of pixels is reversed effectively mirroring the image from left to right.
Vertical Flip (Top to Bottom) − In a vertical flip each column of pixels is reversed effectively mirroring the image from top to bottom.
These can be achieved by using the transpose() method of the Image module of the pillow library.
Flipping images using the transpose() method
The transpose() method in Pillow is used to manipulate the orientation of an image. It allows us to perform various transformations on the image such as rotating and mirroring by specifying the type of transpose operation we want to apply.
The following is the syntax and parameters of the transpose() method.
PIL.Image.transpose(method)
Where,
-
method − The transpose method to be applied. It can take one of the following values −
Image.Transpose.ROTATE_90 − Rotate the image 90 degrees counterclockwise.
Image.Transpose.ROTATE_180 − Rotate the image 180 degrees.
Image.Transpose.ROTATE_270 − Rotate the image 90 degrees clockwise.
Image.Transpose.FLIP_LEFT_RIGHT − Flip the image horizontally i.e. left to right.
Image.Transpose.FLIP_TOP_BOTTOM − Flip the image vertically i.e. top to bottom.
Following is the input image used in all the examples of this chapter.

Example
In this example we are using the transpose() method to flip the input image by 90 degrees counterclockwise.
from PIL import Image #Open an image image = Image.open("Images/image_.jpg") #Rotate the image 90 degrees counterclockwise rotated_image = image.transpose(Image.Transpose.ROTATE_90) #Save or display the rotated image rotated_image.save("output Image/rotated_image.jpg") open_rotated_image = Image.open("output Image/rotated_image.jpg") open_rotated_image.show()
Output

Example
In this example we are using the transpose() method to flip the input image by horizontally.
from PIL import Image #Open an image image = Image.open("Images/image_.jpg") #Flip the image horizontally (left to right) flipped_image = image.transpose(Image.Transpose.FLIP_LEFT_RIGHT) #Save or display the rotated image flipped_image.save("output Image/flipped_image.jpg") open_flipped_image = Image.open("output Image/flipped_image.jpg") open_flipped_image.show()
Output

Example
In this example we are using the transpose() method by using FLIP_TOP_BOTTOM parameter to flip the input image by vertically.
from PIL import Image #Open an image image = Image.open("Images/image_.jpg") #Flip the image horizontally (left to right) vflipped_image = image.transpose(Image.Transpose.FLIP_TOP_BOTTOM) #Save or display the rotated image vflipped_image.save("output Image/vflipped_image.jpg") open_flipped_image = Image.open("output Image/vflipped_image.jpg") open_flipped_image.show()
Output

Rotating Images
Rotating an image in Pillow (Python Imaging Library) refers to the process of changing the orientation of an image by a specific angle. This can be useful for various purposes such as correcting the orientation of photos, creating special effects or aligning images.
Pillow provides the rotate() method to perform this operation. The below are the points we need to know about rotating images in Pillow.
Rotate by Angle − The rotate() method allows us to specify the angle in degrees by which we want to rotate the image. A positive angle rotates the image counterclockwise and a negative angle rotates it clockwise.
Canvas Expansion − By default when we rotate an image it may not fit entirely within the original image canvas and which can lead to cropping. Then we can use the expand parameter to ensure that the rotated image fits within the canvas without being cropped.
Rotating images using the rotate() method
The rotate() method in Image module is used to perform rotation of the given input image.
Here the following is the basic syntax of the rotate() method.
PIL.Image.rotate(angle, expand=False, resample=3)
Where,
angle − This parameter specifies the angle of rotation in degrees. Positive values rotate the image counterclockwise while negative values rotate it clockwise.
expand (optional) − If it is set to True it allows the image canvas to be expanded to ensure that the entire rotated image fits within it. If it is set to 'False' (the default) the image is cropped to fit within the original canvas.
resample (optional) − An optional resampling filter. The default value is '3' which corresponds to the anti-aliased high-quality filter. We can choose from various resampling filters such as 'Image.NEAREST', 'Image.BOX', 'Image.BILINEAR', 'Image.HAMMING', 'Image.BICUBIC', 'Image.LANCZOS', etc.
Following is the input image used in all the examples of this chapter.

Example
In this example we are using the rotate() method for rotating the given input image by 45 degrees.
from PIL import Image #Open an image image = Image.open("Images/flowers.jpg") #Rotate the image by 45 degrees counterclockwise rotated_image = image.rotate(45) #Save the rotated image rotated_image.save("output.jpg") rotated_image.show()
Output

Example
Here in this example we are rotating the input image by ensuring the rotated image fits within the canvas without cropping. This can be achieved by setting the expand parameter to True.
from PIL import Image #Open an image image = Image.open("Images/flowers.jpg") #Rotate the image by 45 degrees and expand the canvas to fit the entire rotated image rotated_image = image.rotate(45, expand=True) rotated_image.show()
Output

Example
In this example we are rotating the image into anticlockwise by passing negative value -45 degrees as parameter to the rotate() method.
from PIL import Image #Open an image image = Image.open("Images/flowers.jpg") #Rotate the image by 45 degrees and expand the canvas to fit the entire rotated image rotated_image = image.rotate(-45, expand=True) rotated_image.show()
Output
