Replacing A Quadrilateral With A Square Generated From Pil.quad
I am working on a computer vision problem involving some basic transformations and could use your help. Input image: Transformed image I understand that we take this quadrilatera
Solution 1:
phew! I figured this out using this helpful answer at Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]
While you use QUAD to go from quadrilateral
to rectangle
, you can use perspective to go back from rectangle
to quadrilateral
#!/usr/bin/env python3from itertools import chain
from wand.color import Color
from wand.image import Image
with Image(filename='image2') as cover, Image(filename='image3') as template:
w, h = cover.size
cover.virtual_pixel = 'transparent'
source_points = (
(0, 0),
(w, 0),
(w, h),
(0, h)
)
destination_points = (
(628+78.37203406, 35.24937345),
(628+577.65062655, 62.72203406),
(628+550.17796594, 562.00062655),
(628+50.89937345, 534.52796594)
)
order = chain.from_iterable(zip(source_points, destination_points))
arguments = list(chain.from_iterable(order))
cover.distort('perspective', arguments)
# Overlay cover onto template and save
template.composite(cover,left=0,top=0)
template.save(filename='result.png')
Post a Comment for "Replacing A Quadrilateral With A Square Generated From Pil.quad"