Skip to content

Translation invariance - #10575

Open
strike19 wants to merge 3 commits into
python-pillow:mainfrom
strike19:translation-invariance
Open

strike19 wants to merge 3 commits into
python-pillow:mainfrom
strike19:translation-invariance

Conversation

@strike19

Copy link
Copy Markdown

Make ImageDraw translation invariant at negative coordinates

Fixes #10228.

Drawing a shape, then drawing it again moved by a whole number of pixels and cropping back, should give the same
pixels. ImageDraw does not do this once a shape extends past the left or top edge, and for some shapes also with
integer coordinates. Two causes:

  • Fractional coordinates are converted with a C cast, which truncates towards zero, and ROUND_UP/ROUND_DOWN,
    lround() and roundf() round halves away from zero. Both treat the origin differently from every other position.
  • The x of a polygon edge on a scanline is computed in float (dx * dy), so whether a pixel at an integer-coordinate
    edge is set depends on float error, and therefore on the position.

Changes

Commit 1:

  • _imaging.c: convert coordinates with floor() in arc, bitmap, chord, ellipse, line, point, pieslice, polygon and
    rectangle. This also covers dash.
  • Draw.c: round scanline edges with halves towards +inf (ROUND_RIGHT/ROUND_LEFT). The symmetric macros stay for
    wide line offsets, which are differences of coordinates. floor for outline edges, curve flattening, transforms,
    and the pieslice outline and clip tree. The edge crossing is computed from exact integer numerators in double.
  • ImageDraw.py: round rounded_rectangle() corners half up instead of half to even, and split the text position with
    floor so the fractional part is always in [0, 1).

Commit 2: ImageDraw.Outline stores, parses and flattens curves in double instead of float. A curve moved by 1000
could set different pixels, also with integer coordinates.

Output for coordinates >= 0

For coordinates >= 0, floor equals the cast and ROUND_RIGHT/ROUND_LEFT equal ROUND_UP/ROUND_DOWN, so output
only changes in two cases. Of 88,000 random images with positive coordinates, 341 changed:

  • 247 rounded_rectangle() with a coordinate of exactly x.5, e.g. rounded_rectangle((2.5, 2.5, 12.5, 12.5), 3) now
    sets pixels (3, 3, 14, 14) instead of (2, 2, 13, 13). Half to even cannot be translation invariant (2.5 -> 2,
    3.5 -> 4).
  • 94 edges of polygons, regular polygons, wide lines and outlines, where the float crossing was on the wrong side of a
    pixel.

No reference image in the test suite changes. Runtime is unchanged (polygon with 2,000 vertices on 4096 x 4096: fill
123 -> 120 ms, outline 124 -> 120 ms, wide line 37 -> 36 ms).

Tests

test_translation_invariant draws 60 random shapes of 20 kinds (including dash lines, polygon and rectangle
outlines) at integer and fractional coordinates, moved by 10 and 63 pixels, and compares with the unmoved image.
Without this PR, 48 of the 80 cases fail on main (macOS arm64). Two more tests cover a rectangle at (-1, -1, 0, 0)
and a curve moved by 1000.

Tested on macOS arm64 (clang, Python 3.14) on current main. Before the dash cases were added, also on Windows 11
x86_64 (MSVC) and Linux x86_64, on main from a few days earlier with the same patch.

A question about crop()

#10243 made crop() translation invariant for 13.1.0. Its title, the release notes ("always rounded up") and the
comment in test_crop_float_half say halves are rounded up, but the code rounds them towards -inf: crop() from 1.5
starts at pixel 1, from 2.5 at pixel 2, from -0.5 at pixel -1. The test checks the size of the result, not its
position, so it passes either way.

This PR rounds halves towards +inf, as the documentation of #10243 describes. So a rectangle from 2.5 to 7.5 sets
pixels 3 to 7, but crop() with the same box takes pixels 2 to 6. Both are translation invariant. Which direction
should Pillow use for positions? If "up" is intended, the crop() code would need to change; if "down", I can change
this PR to match.

🤖 Generated with Claude Code

strike19 and others added 2 commits November 20, 2026 20:35
Fractional coordinates were converted with a C cast, which truncates
towards zero, and ROUND_UP/ROUND_DOWN, lround() and roundf() round halves
away from zero. Both treat the origin differently from every other
position, so a shape extending past the left or top edge was drawn
differently from the same shape moved by a whole number of pixels and
then cropped.

* _imaging.c: convert coordinates with floor() in arc, bitmap, chord,
  ellipse, line, point, pieslice, polygon and rectangle, so this also
  applies to lines and outlines drawn with dash.
* Draw.c: round scanline edges with halves towards +inf (ROUND_RIGHT/
  ROUND_LEFT) instead of symmetric around zero; the symmetric macros stay
  for wide line offsets, which are differences of coordinates. Use floor
  for outline edges, curve flattening and transforms, and for the pieslice
  outline and clip tree.
* Draw.c: compute the x of an edge on a scanline from exact integer
  numerators in double precision, instead of float dx * dy. Whether a
  pixel is set at an integer-coordinate polygon edge no longer depends on
  float error.
* ImageDraw.py: round rounded_rectangle() corners half up, and split the
  text position with floor so the fractional part is always in [0, 1).

For coordinates >= 0, output only changes for rounded_rectangle() at a
coordinate halfway between two integers, such as 2.5 (previously rounded
half to even), and for edges of polygons, regular polygons, wide lines
and outlines that float error put on the wrong side of a pixel.

Fixes python-pillow#10228

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ImageDraw.Outline parsed its coordinates as float and flattened curves
in float, so the rounding error depended on the position. A curve moved
by a whole number of pixels could set different pixels, also with
integer coordinates (e.g. when moved by 1000).

Store and parse outline coordinates as double and flatten in double.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@strike19
strike19 force-pushed the translation-invariance branch from 0ce6a17 to f7eca4e Compare November 20, 2026 19:36
@radarhere radarhere added the 🤖-assisted AI-assisted label Nov 21, 2026
# Conflicts:
#	docs/releasenotes/13.1.0.rst

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

🤖-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)

3 participants