From d72c31d70203f5dfb2e48a0a13e71ba55144f2cb Mon Sep 17 00:00:00 2001 From: tony1233200 Date: Thu, 19 Nov 2026 18:52:47 +0000 Subject: [PATCH 1/2] Make ImageDraw line and polygon drawing translation-invariant for negative coordinates Line and polygon coordinates were converted from float to int by truncation toward zero, and wide-line edges were rounded around zero, so a shape extending past the left or top edge of an image rasterised differently from the same shape shifted by an integer offset: geometry lying entirely at negative coordinates could still set pixels in column/row 0. Convert line and polygon coordinates with floor() instead, and round wide-line edges half away from -inf, so rasterisation only depends on the fractional position of the geometry. Fixes #10228. --- Tests/test_imagedraw.py | 33 +++++++++++++++++++++++++++++++++ docs/releasenotes/13.1.0.rst | 9 +++++++++ src/_imaging.c | 30 +++++++++++++++++++----------- src/libImaging/Draw.c | 9 +++++---- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 28c965c562b..d7c846c629f 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1846,3 +1846,36 @@ def test_dash_empty() -> None: with pytest.raises(ValueError, match="dash must be a non-empty tuple of ints"): draw.rectangle([10, 10, 90, 90], dash=()) + + +def test_negative_coordinates_translation_invariance() -> None: + # Drawing a shape must not depend on where the origin is: a shape drawn + # at a negative offset and the same shape drawn shifted by an integer + # offset, then cropped back, must produce identical pixels. + shift = 10 + width = height = 12 + + def draw( + kind: str, pts: list[tuple[float, float]], dx: int, line_width: int = 0 + ) -> Image.Image: + im = Image.new("L", (width + shift, height), 0) + d = ImageDraw.Draw(im) + shifted = [(x + dx, y) for x, y in pts] + if kind == "polygon": + d.polygon(shifted, fill=255, outline=255) + else: + d.line(shifted, fill=255, width=line_width) + return im.crop((dx, 0, dx + width, height)) + + shapes: list[tuple[str, list[tuple[float, float]], int]] = [ + # A sliver lying entirely left of x = 0 + ("polygon", [(-0.9, 2), (-0.2, 2), (-0.2, 8), (-0.9, 8)], 0), + # A box whose left edge is at x = -0.3 + ("polygon", [(-0.3, 2), (5.5, 2), (5.5, 8), (-0.3, 8)], 0), + # A vertical line, 1 px wide, centred at x = -0.6 + ("line", [(-0.6, 2), (-0.6, 9)], 1), + # A vertical line, 3 px wide, centred at x = -1.6 + ("line", [(-1.6, 2), (-1.6, 9)], 3), + ] + for kind, pts, line_width in shapes: + assert_image_equal(draw(kind, pts, 0, line_width), draw(kind, pts, shift, line_width)) diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 22759b2fc72..b14d73356de 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -67,6 +67,15 @@ Two new image processing operations are available in ImageOps. Other changes ============= +Negative ImageDraw coordinates +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``ImageDraw`` line and polygon coordinates are now rounded toward negative +infinity instead of toward zero, and wide line edges are rounded in the same +way at every position. Drawing a shape that extends past the left or top edge +of an image now produces the same pixels as drawing it shifted into the image +and cropping back. + Rounding crop coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/_imaging.c b/src/_imaging.c index f6a29515269..6845d20e422 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -102,6 +102,10 @@ #include #include +/* ImageDraw coordinates are rounded toward -inf, so that drawing a shape + does not depend on where the origin sits relative to the shape. */ +#define IMAGING_FLOOR(v) ((int)floor(v)) + #define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1]) #define L16(p, i) ((((int)p[(i) + 1]) << 8) + p[(i)]) #define S16(v) ((v) < 32768 ? (v) : ((v) - 65536)) @@ -3783,10 +3787,10 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) { p = &xy[i + i]; if (ImagingDrawLine( self->image->image, - (int)p[0], - (int)p[1], - (int)p[2], - (int)p[3], + IMAGING_FLOOR(p[0]), + IMAGING_FLOOR(p[1]), + IMAGING_FLOOR(p[2]), + IMAGING_FLOOR(p[3]), &ink, self->blend, dash, @@ -3798,7 +3802,11 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) { } if (p) { /* draw last point */ ImagingDrawPoint( - self->image->image, (int)p[2], (int)p[3], &ink, self->blend + self->image->image, + IMAGING_FLOOR(p[2]), + IMAGING_FLOOR(p[3]), + &ink, + self->blend ); } } else { @@ -3806,10 +3814,10 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) { double *p = &xy[i + i]; if (ImagingDrawWideLine( self->image->image, - (int)p[0], - (int)p[1], - (int)p[2], - (int)p[3], + IMAGING_FLOOR(p[0]), + IMAGING_FLOOR(p[1]), + IMAGING_FLOOR(p[2]), + IMAGING_FLOOR(p[3]), &ink, width, self->blend, @@ -3990,8 +3998,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) { } for (i = 0; i < n; i++) { - ixy[i + i] = (int)xy[i + i]; - ixy[i + i + 1] = (int)xy[i + i + 1]; + ixy[i + i] = IMAGING_FLOOR(xy[i + i]); + ixy[i + i + 1] = IMAGING_FLOOR(xy[i + i + 1]); } free(xy); diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index b344aa4e05f..3ea39d64023 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -61,11 +61,12 @@ ink16(Imaging im, const void *ink_) { } /* - * Rounds around zero (up=away from zero, down=towards zero) - * This guarantees that ROUND_UP|DOWN(f) == -ROUND_UP|DOWN(-f) + * Rounds half away from -inf (up = floor(f + 0.5), down = ceil(f - 0.5)), + * so the result only depends on the fractional position of f, not on its + * sign. This keeps rasterisation translation-invariant across the origin. */ -#define ROUND_UP(f) ((int)((f) >= 0.0 ? floor((f) + 0.5F) : -floor(fabs(f) + 0.5F))) -#define ROUND_DOWN(f) ((int)((f) >= 0.0 ? ceil((f) - 0.5F) : -ceil(fabs(f) - 0.5F))) +#define ROUND_UP(f) ((int)floor((f) + 0.5F)) +#define ROUND_DOWN(f) ((int)ceil((f) - 0.5F)) /* -------------------------------------------------------------------- */ /* Primitives */ From 2fd2fc3a64f17055b41c0244084525ddc6919b2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:07:21 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_imagedraw.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index d7c846c629f..53d048ea64b 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1878,4 +1878,6 @@ def draw( ("line", [(-1.6, 2), (-1.6, 9)], 3), ] for kind, pts, line_width in shapes: - assert_image_equal(draw(kind, pts, 0, line_width), draw(kind, pts, shift, line_width)) + assert_image_equal( + draw(kind, pts, 0, line_width), draw(kind, pts, shift, line_width) + )