Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@homm
Copy link
Member

@homm homm commented Dec 7, 2019

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.

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.

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]
Copy link
Member

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.

Copy link
Member

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)

Copy link
Member

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

@radarhere radarhere merged commit 52113a8 into python-pillow:master Dec 16, 2019
@homm homm deleted the fix-thumbnail-aspect-ratio branch December 16, 2019 10:58
radarhere added a commit to radarhere/Pillow that referenced this pull request Dec 17, 2019
hugovk added a commit that referenced this pull request Dec 17, 2019
kernc added a commit to kernc/pelican-plugins that referenced this pull request Jul 19, 2020
Due to change in pillow aspect rounding:
* python-pillow/Pillow#4256
kdeldycke pushed a commit to pelican-plugins/thumbnailer that referenced this pull request Aug 28, 2020
Due to change in pillow aspect rounding:
* python-pillow/Pillow#4256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.