-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Better thumbnail aspect ratio preservation #4256
Better thumbnail aspect ratio preservation #4256
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So using round
to round to the nearest integer, instead of int
to round down to an integer.
>>> int(1.1)
1
>>> int(1.9)
1
>>> round(1.1)
1
>>> round(1.9)
2
Let's also add this to the release notes.
y = int(max(y * size[0] / x, 1)) | ||
x = int(size[0]) | ||
y = max(round(y * size[0] / x), 1) | ||
x = size[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be aware - by removing the int
rounding on these second lines, if someone had existing code im.thumbnail((150.2, 150.2))
, it would now break.
That's not unreasonable though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example:
from PIL import Image
im = Image.open("Tests/images/hopper.png")
print(im.size)
im.thumbnail((15.2, 15.2))
print(im.size)
im.thumbnail((10, 10))
print(im.size)
On 6.2.1:
(128, 128)
(15, 15)
(10, 10)
On master with this PR:
(128, 128)
Traceback (most recent call last):
File "1.py", line 6, in <module>
im.thumbnail((15.2, 15.2))
File "/Users/hugo/github/Pillow/src/PIL/Image.py", line 2153, in thumbnail
im = self.resize(size, resample)
File "/Users/hugo/github/Pillow/src/PIL/Image.py", line 1822, in resize
return self._new(self.im.resize(size, resample, box))
TypeError: integer argument expected, got float
Should we fix it something like this?
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index 613b3a36..981c8c81 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -2138,10 +2138,10 @@ class Image:
x, y = self.size
if x > size[0]:
y = max(round(y * size[0] / x), 1)
- x = size[0]
+ x = round(size[0])
if y > size[1]:
x = max(round(x * size[1] / y), 1)
- y = size[1]
+ y = round(size[1])
size = x, y
if size == self.size:
(128, 128)
(15, 15)
(10, 10)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I've created PR #4272
Due to change in pillow aspect rounding: * python-pillow/Pillow#4256
Due to change in pillow aspect rounding: * python-pillow/Pillow#4256
If the source image is 256×162, it's aspect ratio is 1.58. Before, after thumbnailing, for example, the resulting image had 33×20 size, which aspect ratio is 1.65. Now thumbnail returns 33×21 image, with aspect ration 1.57, which is much closer.