Skip to content

Make ImageDraw line and polygon drawing translation-invariant for negative coordinates - #10564

Closed
tony1233200 wants to merge 2 commits into
python-pillow:mainfrom
tony1233200:fix/10228-imagedraw-floor
Closed

tony1233200 wants to merge 2 commits into
python-pillow:mainfrom
tony1233200:fix/10228-imagedraw-floor

Conversation

@tony1233200

Copy link
Copy Markdown
Contributor

Fixes #10228.

The problem

ImageDraw converted float coordinates to integers by truncation toward
zero, and wide-line edges were rounded "around zero" (ROUND_UP/ROUND_DOWN
in Draw.c mirrored the result for negative values). Rasterisation therefore
depended on where the origin sat relative to the shape: a polygon lying
entirely left of x = 0 (from -0.9 to -0.2) still set 7 pixels in column 0,
while the same polygon shifted right by 10 px set none. Anyone drawing tiled
geometry (map tiles, cropped views) gets different pixels depending on the
tile offset.

Reproduction from the issue, before this change:

polygon x -0.9 .. -0.2     offset 0: 7 px set,  offset 10: 0 px set,  differ: 7
polygon x -0.3 .. 5.5      offset 0: 42 px set, offset 10: 42 px set, differ: 0
line x = -0.6, width 1     offset 0: 8 px set,  offset 10: 0 px set,  differ: 8
line x = -1.6, width 3     offset 0: 8 px set,  offset 10: 0 px set,  differ: 8

After this change all four cases are identical (differ: 0).

The change

  • src/_imaging.c: line, wide-line and polygon coordinates are converted
    with floor() (IMAGING_FLOOR) instead of a truncating (int) cast.
  • src/libImaging/Draw.c: ROUND_UP(f) is now floor(f + 0.5) and
    ROUND_DOWN(f) is ceil(f - 0.5) at every position, not mirrored around
    zero, so wide-line edge rounding is translation-invariant across the
    origin.
  • Scope is lines and polygons (the shapes reported in ImageDraw is not translation-invariant for negative coordinates (truncation toward zero instead of floor) #10228); rectangle,
    ellipse, arc and pieslice coordinate handling is unchanged.
  • Release note added under "Other changes" in docs/releasenotes/13.1.0.rst.

Tests

  • New regression test test_negative_coordinates_translation_invariance in
    Tests/test_imagedraw.py, covering the four shapes from the issue. It
    fails on the old code and passes with this change.
  • python3 selftest.py: 59 tests passed.
  • Tests/test_imagedraw.py: 273 passed, 1 xfailed (pre-existing).
  • Tests/test_imagedraw2.py + Tests/test_imagepath.py: 92 passed.
  • Tests/test_imagedraw_i16.py + Tests/test_psdraw.py: 60 passed.

Disclosure: this fix was prepared with AI assistance.

tony1233200 and others added 2 commits November 19, 2026 18:52
…ative 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 python-pillow#10228.
@radarhere radarhere added the 🤖-assisted AI-assisted label Nov 19, 2026
@radarhere

Copy link
Copy Markdown
Member

Closing, as #10575 was created by the author of the issue, and contains tests that do not pass in this PR.

@radarhere radarhere closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageDraw is not translation-invariant for negative coordinates (truncation toward zero instead of floor)

2 participants