Open In App

Wand perspective distortion method in Python

Perspective is another type of distortion method in distort function. It represents image in perspective based on arguments. Perspective distortion requires 4 pairs of points which is a total of 16 doubles. The order of the arguments is groups of source & destination coordinate pairs.
 

src1x, src1y, dst1x, dst1y,
src2x, src2y, dst2x, dst2y,
src3x, src3y, dst3x, dst3y,
src4x, src4y, dst4x, dst4y

Syntax: 
 



wand.image.distort('perspective', arguments')

Source Image: 
 



Example #1:
 




# Import Color from wand.color module
from wand.color import Color
# Import Image from wand.image module
from wand.image import Image
 
with Image(filename ='gog.png') as img:
    img.virtual_pixel = 'background'
    img.background_color = Color('green')
    img.matte_color = Color('skyblue')
    arguments = (0, 0, 20, 60,
                 90, 0, 70, 63,
                 0, 90, 5, 83,
                 90, 90, 85, 88)
    # perspective distortion using distort function
    img.distort('perspective', arguments)
    img.save(filename ='gfg_p.png')

Output: 
 

Example #2: Splitting source and destination points using itertools chain
 




from itertools import chain
# Import Color from wand.color module
from wand.color import Color
# Import Image from wand.image module
from wand.image import Image
 
with Image(filename ='gog.png') as img:
    img.background_color = Color('skyblue')
    img.virtual_pixel = 'background'
    sp = (
        (0, 0),
        (140, 0),
        (0, 92),
        (140, 92)
    )
    dp = (
        (14, 4.6),
        (126.9, 9.2),
        (0, 92),
        (140, 92)
    )
    order = chain.from_iterable(zip(sp, dp))
    arguments = list(chain.from_iterable(order))
    img.distort('perspective', arguments)
    img.save(filename ='gfg_p2.png')

Output: 
 

 


Article Tags :