📜  Python中的魔杖透视变形方法

📅  最后修改于: 2022-05-13 01:55:07.043000             🧑  作者: Mango

Python中的魔杖透视变形方法

透视是失真函数中的另一种失真方法。它基于参数以透视方式表示图像。透视变形需要 4 对点,总共 16 个双精度点。参数的顺序是源和目标坐标对组。

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

句法:

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

源图像:

示例 #1:

Python3
# 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')


Python3
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')


输出:

示例 #2:使用 itertools 链拆分源点和目标点

Python3

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')

输出: