GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix size calculation of Image.thumbnail() by orlnub123 · Pull Request #4404 · python-pillow/Pillow

Conversation

@orlnub123

@orlnub123 orlnub123 commented Feb 4, 2020

Copy link
Copy Markdown
Contributor

The current size calculation is fine if only one axis is smaller or if the image is wide, but it trips up when both axes are smaller and the image is tall. Here's an example:

from PIL import Image

im = Image.new("L", (145, 100))
im.thumbnail((50, 50))
assert im.size == (50, 34)  # This is fine, the image is wide.

im = Image.new("L", (100, 145))
im.thumbnail((100, 50))
assert im.size == (34, 50)  # Also fine, only one axis is smaller.

im = Image.new("L", (100, 145))
im.thumbnail((50, 50))
assert im.size == (34, 50)  # Oh no! It returned (35, 50)!

Someone made a pull request that fixed this in 2016, but it looks like he closed it shortly after due to a CI failure.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@python-pillow python-pillow deleted a comment from codecov Bot Feb 5, 2020
@radarhere

radarhere commented Feb 5, 2020

Copy link
Copy Markdown
Member

I imagine that @homm, as the author of #4256, would like to review this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

homm
homm reviewed Feb 5, 2020
View reviewed changes

@homm homm left a comment

Copy link
Copy Markdown
Member

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.

Looks like this happened due to double rounding in both branches.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/PIL/Image.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

homm
homm approved these changes Feb 7, 2020
View reviewed changes
@orlnub123

orlnub123 commented Feb 9, 2020

Copy link
Copy Markdown
Contributor Author

These latest commits have a fix related to the issue previously discussed. Rounding up the size contradicts the documentation which states:

This method modifies the image to contain a thumbnail version of itself, no larger than the given size.

So now when you request a thumbnail with a maximum size of (99.9, 99.9), you get back an image with the size of (99, 99) instead of (100, 100).

As far as I can tell passing float values to size is deprecated, so that commit can be reverted when the switch is made.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@orlnub123
orlnub123 requested a review from homm February 9, 2020 01:32
@radarhere
radarhere force-pushed the bugfix/thumbnail branch 2 times, most recently from 5f0d846 to 7d3c9b0 Compare February 16, 2020 10:22
orlnub123 added 5 commits February 16, 2020 21:25
@orlnub123 @radarhere
Fix size calculation of Image.thumbnail()
e9ef1d2
@orlnub123 @radarhere
Don't upscale if the image is smaller than the size
e226e4e
@orlnub123 @radarhere
Add test against upscaling
6d3c7d6
@orlnub123 @radarhere
Floor the size
2e716de
@orlnub123 @radarhere
Fix test
64c08f4
@radarhere
radarhere force-pushed the bugfix/thumbnail branch from 7d3c9b0 to 64c08f4 Compare February 16, 2020 10:26
@homm

homm commented Feb 20, 2020

Copy link
Copy Markdown
Member

I have an interesting question. Let's say we have an image 100×30 pixels and requested 75×75 thumbnail. The result will be 75×22.5 which will be rounded to 75×22. It seems that there should be no difference in which direction we're rounding, up or down, both options should be equal.

But the thing is, 100/30 = 3.3(3), 75/22 = 3.4(09) and 75/23 ≈ 3.261 and this is a bit closer to 100/30, but we still have an answer 75×22. Why?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@orlnub123
Improve rounding
8f21d0d
@orlnub123

orlnub123 commented Feb 21, 2020

Copy link
Copy Markdown
Contributor Author

I'd like to point out that the current implementation had the same issue. It happens because the rounding is indiscriminate towards the aspect ratio. I've added a commit that fixes the issue, improvements are welcome.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

homm
homm reviewed Feb 22, 2020
View reviewed changes
Comment thread src/PIL/Image.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hugovk hugovk mentioned this pull request Feb 22, 2020
Use Codecov bash because the Action times out on macOS #4444
Merged
@orlnub123
Make key an argument
84c33ab
homm
homm approved these changes Feb 25, 2020
View reviewed changes
@hugovk
hugovk merged commit 3f9b615 into python-pillow:master Mar 5, 2020
@hugovk

hugovk commented Mar 5, 2020

Copy link
Copy Markdown
Member

Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StatusWrike StatusWrike mentioned this pull request Nov 9, 2020
Bump pillow from 5.1.0 to 7.1.0 in /test/appium status-im/status-legacy#11409
Closed
@sync-by-unito sync-by-unito Bot mentioned this pull request Feb 1, 2021
Bump pillow from 4.3.0 to 7.1.0 Gunnerus/gunnerus#415
Closed
This was referenced Mar 6, 2021
Bump pillow from 6.2.0 to 7.1.0 xu-weizhen/Beautify-by-PaddleHub#3
Merged
Bump pillow from 6.2.1 to 7.1.0 vikky12343/ConnectingGeeks#4
Closed
Bump pillow from 5.4.1 to 7.1.0 gring2/bookMarkLibrary#5
Closed
Bump pillow from 6.2.1 to 7.1.0 SunshineList/Blog#8
Closed
Bump pillow from 4.1.1 to 7.1.0 Josephpaik/myscanutils#2
Closed
This was referenced Mar 17, 2021
Bump pillow from 5.2.0 to 7.1.0 ecss-soton/ecssweb#65
Closed
Bump pillow from 3.1.2 to 7.1.0 Rockks01/didactic-eureka#7
Closed
Bump pillow from 7.0.0 to 7.1.0 yurippe/mangakakalot-downloader#4
Merged
Bump pillow from 7.0.0 to 7.1.0 ENCODE-DCC/snovault#325
Closed
Bump pillow from 6.1.0 to 7.1.0 a-z-e-r-i-l-a/simulation-tools#1
Closed
Bump pillow from 4.0.0 to 7.1.0 in /dog-project/requirements tiandiao123/deep_learning#120
Merged
Bump pillow from 7.0.0 to 7.1.0 koulanurag/visTorch#3
Closed
Bump pillow from 4.0.0 to 7.1.0 in /study_material/intro-to-tensorflow tiandiao123/deep_learning#121
Merged
Bump pillow from 4.0.0 to 7.1.0 in /study_material/intro-to-tflearn tiandiao123/deep_learning#122
Merged
Bump pillow from 7.0.0 to 7.1.0 luanaflorencio/Projeto-Pesquisa#2
Closed
Bump pillow from 6.0.0 to 7.1.0 MarulandaT/Observatorio-Egresados#4
Closed
Bump pillow from 6.2.1 to 7.1.0 brekelma/dsbox_graphs#3
Closed
Bump pillow from 6.0.0 to 7.1.0 jhochmuth/Botokovich#2
Closed
Bump pillow from 5.1.0 to 7.1.0 in /docker_python roberto-goncalves/devops-integration#4
Closed
Bump pillow from 6.2.1 to 7.1.0 xdyxiang/mytool_pyqt5#4
Closed
Bump pillow from 5.1.0 to 7.1.0 in /dtr_code uwsampl/dtr-prototype#2
Closed
Bump pillow from 6.0 to 7.1.0 Digital-Preservation-Finland/file-scraper#6
Closed
Bump pillow from 6.0 to 7.1.0 Digital-Preservation-Finland/file-scraper#7
Closed
Bump pillow from 7.0.0 to 7.1.0 charalambospapa/socialblog#2
Merged
Bump pillow from 6.1.0 to 7.1.0 in /02_Sensive_Blog_Templates j0hntv/website-layout-for-pydev#5
Closed
Bump pillow from 6.2.0 to 7.1.0 Freedomisgood/NJUPT_Autojudge#5
Closed
Bump pillow from 5.0.0 to 7.1.0 karansthr/django_tinymce_demo#5
Closed
Bump pillow from 7.0.0 to 7.1.0 zecollokaris/Django-shell-script#4
Open
Bump pillow from 5.3.0 to 7.1.0 i13-msrg/cidds#5
Closed
Bump pillow from 4.2.1 to 7.1.0 muladzevitali/alpr_detection_and_recognition#10
Closed
Bump pillow from 6.2.0 to 7.1.0 pb-brainiac/semseg_od#2
Closed
Bump pillow from 6.2.1 to 7.1.0 edwardpraveen/ImageScrapper-Heroku#2
Closed
chore(deps): Bump pillow from 7.0.0 to 7.1.0 yuzhanglong/EasyQuestionnaire-backend#2
Merged
Bump pillow from 7.0.0 to 7.1.0 timshin43/resume_website#3
Closed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 4, 2026
Codex/repair grouping and stage work scherenhaenden/photo-culler#24
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@homm homm homm approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@orlnub123 @radarhere @homm @hugovk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.