Repository navigation
Image.crop() size depends on position when a box edge is at .5 (round half to even) #10242
Unanswered
sungbin1015
asked this question in
Q&A
Replies: 1 comment
|
A similar issue has been raised about drawing shapes in #10228 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I was checking how we turn float boxes from an object detector into
crop()calls and noticed thatcrop()rounds each edge separately with Python'sround(). Since Python 3 rounds halves to even, a box of the same width can come out a pixel bigger or smaller depending on where it sits. It's easiest to see with a centre-based box of odd width:That's on 13.0.0 and current main. A 1px box at 1.5..2.5 gives an empty image, and the same box shifted by one pixel gives 2px. The rounding was added in #1745 (for #1744) while Pillow still supported Python 2, where
round()rounded halves away from zero, so I suspect the parity effect came with the move to Python 3 rather than being chosen. It's easy for us to work around by rounding before callingcrop(), so nothing is blocked on our side.Is the half-to-even behaviour intended for
crop(), or would you accept a change that rounds halves the same way everywhere, e.g.floor(x + 0.5)?All reactions