Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Tests/test_image_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def test_crop_float() -> None:
assert cropped.size == (3, 5)


@pytest.mark.parametrize("left", (1.5, 2.5, 84.5, 85.5))
def test_crop_float_half(left: float) -> None:
# Halves are rounded up, so a box keeps its width wherever it is
im = Image.new("L", (200, 10))

assert im.crop((left, 0, left + 31, 1)).size == (31, 1)
assert im.crop((left, 0, left + 1, 1)).size == (1, 1)


def test_crop_crash() -> None:
# Image.crop crashes prepatch with an access violation
# apparently a use after free on Windows, see
Expand Down
58 changes: 58 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
xx.y.z
------

Security
========

TODO
^^^^

TODO

:cve:`YYYY-XXXXX`: TODO
^^^^^^^^^^^^^^^^^^^^^^^

TODO

Backwards incompatible changes
==============================

TODO
^^^^

TODO

Deprecations
============

TODO
^^^^

TODO

API changes
===========

TODO
^^^^

TODO

API additions
=============

TODO
^^^^

TODO

Other changes
=============

Rounding crop coordinates
^^^^^^^^^^^^^^^^^^^^^^^^^

:py:meth:`~PIL.Image.Image.crop` rounds float coordinates to the nearest integer.
Coordinates ending in .5 were rounded to the nearest even integer, so a box of the same
size could give an image one pixel larger or smaller depending on where it was. They
are now always rounded up.
1 change: 1 addition & 0 deletions docs/releasenotes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ expected to be backported to earlier versions.
:maxdepth: 2

versioning
13.1.0
13.0.0
12.3.0
12.2.0
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ def _crop(
:returns: A core image object.
"""

x0, y0, x1, y1 = map(int, map(round, box))
x0, y0, x1, y1 = (math.floor(v + 0.5) for v in box)

absolute_values = (abs(x1 - x0), abs(y1 - y0))

Expand Down
Loading