W
W
Wynell_ru2020-04-28 13:50:18
Python
Wynell_ru, 2020-04-28 13:50:18

How does PIL.Image.PERSPECTIVE work?

Good afternoon.
I started to study Pillow and the following question arose:
How does PIL.Image.PERSPECTIVE mode work in img.transform?

img = Image.new("RGB", (100, 100), (155, 155, 155))
draw = ImageDraw.Draw(img)
draw.text((0, 90), "LOLOLOLOLOLOLOL!", font=font)
draw.text((0, 80), "LOLOLOLOLOLOLOL!", font=font)
draw.text((0, 70), "LOLOLOLOLOLOLOL!", font=font)
draw.text((0, 60), "LOLOLOLOLOLOLOL!", font=font)
draw.text((0, 50), "LOLOLOLOLOLOLOL!", font=font)
img.transform((100, 100), Image.PERSPECTIVE, (25, 25, 25, 75, 75, 75, 75, 25))

I thought that it "moves" the corners of the image to the appropriate coordinates, but apparently it doesn't...
gzOzXPw.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-04-28
@Wynell_ru

No, the coordinates are not indicated there, but the coefficients of the equation (ax + by + c) / (gx + hy + 1), (dx + ey + f) / (gx + hy + 1), calculates the coordinates, where to get the value for each pixel (x, y).

From the package documentation:
Data is a 8-tuple (a, b, c, d, e, f, g, h) which contains the coefficients for a perspective transform. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c)/(g x + h y + 1), (d x + e y + f)/(g x + h y + 1) in the input image, rounded to nearest pixel.

Based on this, you can make simple examples:
(1, 0, 0, 0, 1, 0, 0, 0) - will not change the picture
(1, 0, 0, 0, 2, 0, 0, 0) - "flatten "picture 2 times vertically, etc.
To find these coefficients, you can use an auxiliary function, for example, from here https://stackoverflow.com/a/14178717

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question