Skip to content

Add dash parameter for line, polygon, and rectangle drawing#9490

Open
Krishnachaitanyakc wants to merge 5 commits intopython-pillow:mainfrom
Krishnachaitanyakc:add-dashed-line-support
Open

Add dash parameter for line, polygon, and rectangle drawing#9490
Krishnachaitanyakc wants to merge 5 commits intopython-pillow:mainfrom
Krishnachaitanyakc:add-dashed-line-support

Conversation

@Krishnachaitanyakc
Copy link

Summary

Adds a dash parameter to ImageDraw.line(), ImageDraw.polygon(), and ImageDraw.rectangle() that enables drawing dashed outlines, implemented in the Python layer.

  • The dash pattern follows the SVG stroke-dasharray specification: a tuple of ints specifying alternating drawn/blank segment lengths (e.g. (10, 5) draws 10px, skips 5px, repeats)
  • Odd-length patterns are automatically doubled per SVG spec
  • The dash pattern is continuous across connected line segments and polygon/rectangle edges
  • Empty dash tuples raise ValueError
  • Fill is drawn normally; only the outline/stroke is dashed
  • Fully backward compatible: all existing behavior is preserved when dash is not specified

Example usage

from PIL import Image, ImageDraw

im = Image.new("RGB", (200, 200), "white")
draw = ImageDraw.Draw(im)

# Dashed line
draw.line([(10, 100), (190, 100)], fill="black", width=2, dash=(10, 5))

# Dashed rectangle with fill
draw.rectangle([20, 20, 180, 60], fill="lightyellow", outline="blue", width=2, dash=(15, 5, 5, 5))

# Dashed polygon with fill
draw.polygon([(50, 120), (150, 120), (150, 180), (50, 180)], fill="lightblue", outline="red", width=2, dash=(10, 5))

Closes #9127

Test plan

  • Added 10 new tests covering dashed lines, polygons, and rectangles
  • Tests cover: basic dash, multi-segment lines, odd-length patterns, empty dash error, polygon with/without fill, rectangle with/without fill
  • All 281 existing test_imagedraw.py tests continue to pass (backward compatibility verified)
  • Reference images generated and included for pixel-exact assertions
  • Documentation updated for all three methods with versionadded:: 12.2.0

Krishnachaitanyakc and others added 5 commits March 24, 2026 22:31
Add a `dash` parameter to `ImageDraw.line()`, `ImageDraw.polygon()`, and
`ImageDraw.rectangle()` methods that allows drawing dashed outlines.

The dash pattern follows the SVG `stroke-dasharray` specification:
- Accepts a tuple of ints specifying alternating drawn/blank segment lengths
- Odd-length patterns are automatically doubled per the SVG spec
- The dash pattern is continuous across connected line segments and polygon edges
- Empty dash tuples raise ValueError

Closes python-pillow#9127
- Annotate pixels_used as float to fix float/int assignment mismatch
- Rename redefined 'points' variable to 'joint_points' in curve joint code
- Cast flat xy to Sequence[float] before slicing to fix type compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for dashed lines?

1 participant