Skip to content
Related Articles

Related Articles

Wand perspective destortion method in Python
  • Last Updated : 10 May, 2020

Perspective is another type of destortion method in destort 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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :