
- 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 - Applying Perspective Transforms
In Pillow applying perspective transforms involves altering the perspective or viewpoint of an image. This transformation modifies the position of the image's points in a way that simulates a change in perspective such as rotating, skewing or distorting the image.
Perspective Transformation in Pillow
The Pillow's transform() method allows you to apply differenyt types of transformations to an image. For perspective transforms a 3x3 transformation matrix is used to define the transformation. This matrix can represent operations like rotations, translations, scaling and shearing. For perspective transformation the matrix elements control how each pixel's coordinates change.
Matrix Elements for Perspective Transform
The matrix elements (a, b, c, d, e, f, g, h) define the transformation −
a and d represent scaling in the x and y directions.
b and c represent shearing or skewing.
e and f are translations along the x and y axes.
g and h are perspective coefficients.
Applying Perspective Transforms
We can use transform() directly with Image.PERSPECTIVE and pass four source points and four destination points to specify the transformation.
Perspective transformations can distort images significantly. So carefully choose the transformation matrix or points to achieve the desired effect without excessive distortion.
Applying perspective transforms in Pillow involves using transformation matrices or defining source and destination points to alter the image's perspective, simulating changes in angles, orientations or viewpoints. Understanding transformation matrices and point mappings is crucial for achieving the desired visual effects while avoiding excessive distortion or unwanted transformations in the image.
The following are the syntax and parameters of the transform() method.
transformed_image = image.transform(size, method, data, resample=0, fill=0)
Where,
image − The Pillow Image object we want to transform.
size − A tuple representing the output size (width, height) of the transformed image.
method − Defines the type of transformation to be applied Image.AFFINE, Image.PERSPECTIVE or Image.QUAD, etc.
data − Transformation of data required based on the method used.
resample (optional) − The resampling method to use such as Image.BILINEAR, Image.NEAREST and Image.BICUBIC. Default value is 0 i.e. nearest neighbor.
fill (optional) − Fill color for areas outside the transformed image boundaries. Default is 0 i.e. black.
Prepare data based on the Transformation method, for the Image.AFFINE transformation it requires a 6-element tuple that represents the affine transformation matrix. For perspective transformations, Image.PERSPECTIVE is used, which requires an 8-element tuple to represent the perspective transformation matrix. Additionally, Image.QUAD involves providing source and destination quadrilateral points for accurate perspective mapping.
Example
In this example we are performing the perspective transformation on the given input image by using the transform() method.
from PIL import Image import numpy def find_data(source, target): matrix = [] for s, t in zip(source, target): matrix.append([t[0], t[1], 1, 0, 0, 0, -s[0]*t[0], -s[0]*t[1]]) matrix.append([0, 0, 0, t[0], t[1], 1, -s[1]*t[0], -s[1]*t[1]]) A = numpy.matrix(matrix, dtype=float) B = numpy.array(source).reshape(8) res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B) return numpy.array(res).reshape(8) #Open an image image = Image.open("Images/logo_w.png") # image.show() #Define the transformation matrix for perspective transform coeffs = find_data( [(0, 0), (225, 0), (225, 225), (0, 225)], [(15, 115), (140, 20), (140, 340), (15, 250)]) #Applying the perspective transformation transformed_image = image.transform((300, 400), Image.PERSPECTIVE, coeffs, Image.BICUBIC) #Save or display the transformed image transformed_image.save("output Image/transform.png") transformed_image.show()
Output
Following is the input image −

On executing the above code, the following transformed image is displayed −

Example
Here this is another example of performing the perspective transformation to the another image with the matrix elements (a, b, c, d, e, f, g, h).
from PIL import Image #Open an image image = Image.open("Images/flowers.jpg") image.show() #Define the transformation matrix for perspective transform matrix = (10, 4, 11, -1, 5, 2, 1, -1) #Apply the perspective transformation transformed_image = image.transform(image.size, Image.PERSPECTIVE, matrix) #Save or display the transformed image transformed_image.save("output Image/transform_image.jpg") transformed_image.show()
Output
On executing the above code you will get the following output −
Input image:

Output perspective transformed image:

Note
The transform() method supports various transformation types and the transformation data required varies based on the chosen method.
It's crucial to provide the correct transformation data i.e. matrix or points based on the chosen transformation method to achieve the desired effect.
The transform() method in Pillow is versatile and allowing us to perform different types of geometric transformations on images offering flexibility in image manipulation and perspective alterations.