Python Pillow – Applying Perspective Transforms


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 3×3 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 −


logo_w.jpg

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


transform

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:


 flowers input

Output perspective transformed image:


transform_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.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *