Fix PILResizeCrop pre-resize loop using inconsistent height threshold#7106
Open
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Open
Fix PILResizeCrop pre-resize loop using inconsistent height threshold#7106Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
`tools/extra/resize_and_crop_images.py:PILResizeCrop.resize_and_crop_image` does a power-of-two pre-shrink loop before the final thumbnail:
```python
#preresize image with factor 2, 4, 8 and fast algorithm
factor = 1
while img.size[0]/factor > 2*box[0] and img.size[1]2/factor > 2box[1]:
factor *=2
```
The two guards are supposed to check the same thing — "both dimensions are more than 2x the target box" — but the height side multiplies by 2 where the width side does not.
Simplified, the effective per-dimension thresholds are:
So `factor` keeps doubling as long as height > 1x target, not 2x. The loop over-shrinks tall/narrow images (factor grows one extra step past the documented 2x safety margin), which then feeds a smaller-than-intended NEAREST thumbnail into the later ANTIALIAS resize at line 67 — hurting output quality relative to the intended behavior and to the sibling width check.
Root cause
The `*2` on `img.size[1]2/factor` is a stray token left over from the source snippet the comment points to (`http://united-coders.com/christian-harms/image-resizing-tips-every-coder-should-know/\`), where both guards are symmetric (`img.size[i]/factor > 2box[i]` for i in 0, 1). The `2` belongs only with the `box[...]` term on the right-hand side, not multiplied into the image dimension on the left.
Why the fix is correct
Dropping the `*2` restores the documented behavior: both dimensions use the same "still more than 2x the target" guard, so the NEAREST pre-shrink stops at the largest power-of-two factor that leaves enough headroom for the final ANTIALIAS resize on line 67. No other call site or behavior changes — only the height branch of this single guard is corrected.