Skip to content

Round halves up when converting crop coordinates to integers - #10243

Open
sungbin1015 wants to merge 1 commit into
python-pillow:mainfrom
sungbin1015:crop-round-half-up
Open

sungbin1015 wants to merge 1 commit into
python-pillow:mainfrom
sungbin1015:crop-round-half-up

Conversation

@sungbin1015

Copy link
Copy Markdown
Contributor

Follows on from #10242.

Changes proposed in this pull request:

  • Round crop coordinates ending in .5 up, instead of to the nearest even integer
  • Add test_crop_float_half
  • Add a 13.1.0 release note

_crop() uses round(), which since Python 3 sends halves to the nearest even integer. So the same box can come out a pixel larger or smaller depending on where it is:

from PIL import Image

im = Image.new("L", (200, 10))
for left in (84.5, 85.5):
    print(im.crop((left, 0, left + 31, 1)).size)
print(im.crop((1.5, 0, 2.5, 1)).size)
(32, 1)
(30, 1)
(0, 1)

With math.floor(v + 0.5) these are (31, 1), (31, 1) and (1, 1). Coordinates that aren't exactly .5 round as before, so test_crop_float from #1745 is unchanged. #10229 is close in spirit, but it changes how ImageDraw truncates negative coordinates in C and doesn't touch crop().

The full test suite and pre-commit pass locally.

This branch has not been deployed

No deployments
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.

1 participant