Skip to content

Perspective transform question #6317

Answered by radarhere
ktenney asked this question in Q&A
May 22, 2022 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

The official documentation is at https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.transform

When you say it's not working, what do you mean exactly? Here is code that will rotate one of our test images by 90 degrees.

import math
from PIL import Image
im = Image.open("hopper.png")

angle = -math.radians(90)
matrix = [
    round(math.cos(angle), 15),
    round(math.sin(angle), 15),
    0.0,
    round(-math.sin(angle), 15),
    round(math.cos(angle), 15),
    0.0,
    0,
    0,
]
matrix[2] = (1 - matrix[0] - matrix[1]) * im.width / 2
matrix[5] = (1 - matrix[3] - matrix[4]) * im.height / 2

im.transform(im.size, Image.Transform.PERSPECTIVE, matrix).save("rotated_90…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ktenney
Comment options

@radarhere
Comment options

@ktenney
Comment options

Answer selected by radarhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants