Skip to content

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

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

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

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown

@Krishnachaitanyakc Krishnachaitanyakc commented Mar 25, 2026

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

Reference

This implementation follows the SVG stroke-dasharray specification:
https://www.w3.org/TR/SVG2/painting.html#StrokeDashing

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
@radarhere
Copy link
Copy Markdown
Member

Could you link to the SVG specification that you used as a reference?

@Krishnachaitanyakc
Copy link
Copy Markdown
Author

@radarhere updated the description

@radarhere
Copy link
Copy Markdown
Member

Did you use AI to create this PR?

@Krishnachaitanyakc
Copy link
Copy Markdown
Author

Krishnachaitanyakc commented Mar 30, 2026

@radarhere I used AI to plan and implement yes, but did manually verify the changes and tested them

@radarhere radarhere added the 🤖-assisted AI-assisted label Mar 30, 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.

Support for dashed lines?

2 participants