QUAD Deformation #8955
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi. Have you seen https://pillow.readthedocs.io/en/stable/reference/ImageTransform.html? It explains that the parameter you're passing in is
The key word there for this discussion is 'source'. Your expectation was that it was the destination quadrilateral. As far as achieving your effect, could you upload a copy of your initial image? |
Beta Was this translation helpful? Give feedback.
-
To achieve your effect, I found code on StackOverflow. You can find a copy at https://stackoverflow.com/questions/42827978/image-perspective-transform-using-pillow import numpy
from PIL import Image
def find_coeffs(pa, pb):
matrix = []
for p1, p2 in zip(pa, pb):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
A = numpy.matrix(matrix, dtype=float)
B = numpy.array(pb).reshape(8)
res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
return numpy.array(res).reshape(8)
with Image.open("input.png") as im:
coeffs = find_coeffs(
[(50, 100), (100, 500), (600, 400), (650, 50)],
[(0, 0), (im.width, 0), (im.width, im.height), (0, im.height)]
)
im = im.transform(im.size, Image.PERSPECTIVE, coeffs)
im.save("output.png") |
Beta Was this translation helpful? Give feedback.
-
It works SuperWell!!! Screen.Recording.2025-05-15.at.16.08.57.mov |
Beta Was this translation helpful? Give feedback.
To achieve your effect, I found code on StackOverflow. You can find a copy at https://stackoverflow.com/questions/42827978/image-perspective-transform-using-pillow