Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow float center for rotate operations #8114

Merged
merged 1 commit into from
Jun 25, 2024
Merged

Conversation

radarhere
Copy link
Member

In im.rotate(), there is

Pillow/src/PIL/Image.py

Lines 2375 to 2379 in 5bacce9

if center is None:
# FIXME These should be rounded to ints?
rotn_center = (w / 2.0, h / 2.0)
else:
rotn_center = center

Looking through the code after that, there is no reason why they can't be floats - the numbers are simply combined with other floats.

Pillow/src/PIL/Image.py

Lines 2381 to 2399 in 5bacce9

angle = -math.radians(angle)
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,
]
def transform(x, y, matrix):
(a, b, c, d, e, f) = matrix
return a * x + b * y + c, d * x + e * y + f
matrix[2], matrix[5] = transform(
-rotn_center[0] - post_trans[0], -rotn_center[1] - post_trans[1], matrix
)
matrix[2] += rotn_center[0]
matrix[5] += rotn_center[1]

This also allows the rotation center to be the exact center of an image with odd dimensions - although that also sound like a good argument why the user might want to pass in floats as the center, so I've updated the type hint accordingly.

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a seven-year-old FIXME :)

@hugovk hugovk merged commit 5b1a9e1 into python-pillow:main Jun 25, 2024
56 checks passed
@hugovk hugovk added the Cleanup label Jun 25, 2024
@radarhere radarhere deleted the fixme branch June 25, 2024 12:10
@radarhere radarhere changed the title Allow float center for rotate operations to resolve FIXME Allow float center for rotate operations Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants