You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not convinced a need for this to be fast is so widespread - it looks like most of the requests that you've linked to result in answers for how to achieve this in Python, and not comments from people asking for more speed. If you wanted this implemented in Python, that would seem achievable. From #8976, I think you have an above average need for speed.
However, in C, we use a scanline algorithm - for polygons, we're not drawing each line one at a time like a person would, we're determining which pixels should be visible in each horizontal row. To start using dashed lines, I think we would have to take a new approach.
I'm not saying this is impossible, just that it would not be simple. If you are able to post some example code, and precisely specify how fast you need it to run, that would be helpful.
I think that's fair to say. In fact, I have implemented this in Python now, and was surprised to find that performance was completely acceptable. So if you do implement this feature request you don't need to do it for me.
Still, I find it strange that people have to implement this themselves. To me it seems like it should be in the library, but whether you want to do something about that is of course up to you.
I did look at implementing this myself in C, and as far as I can tell the scanlines you're talking about is for filled polygons. For unfilled polygons the draws one line at a time. It was with a view to adding a C implementation of dashing here that I first prototyped it in Python.
In case it will be useful to someone, here's my Python implementation of the SVG algorithm:
classDasher:
def__init__(self, dash_pattern: tuple[int]):
self._dash_pattern=dash_patternself._pixels_used=0# how many pixels of current step used?self._steps=0# how many steps through pattern?defmake_next_step(self, max_length: float) ->float:
current=self._steps%len(self._dash_pattern)
step=min(
self._dash_pattern[current] -self._pixels_used,
max_length
)
self._pixels_used+=stepifself._pixels_used>=self._dash_pattern[current]:
self._pixels_used=0self._steps+=1returnstepdefis_current_dash(self):
returnself._steps%2==0defdist(p1, p2):
returnmath.sqrt((p1[0] -p2[0]) **2+ (p1[1] -p2[1]) **2)
defdraw_dashed_polygon(draw, coords, lc, lw, fc, dashing):
iffc:
draw.polygon(coords, outline=None, width=0, fill=fc)
dasher=Dasher(dashing)
forixinrange(len(coords) -1):
(x, y) =coords[ix]
(target_x, target_y) =coords[ix+1]
rem_length=dist((x, y), (target_x, target_y))
vx= (target_x-x) /rem_lengthvy= (target_y-y) /rem_lengthwhilerem_length>0:
step=dasher.make_next_step(rem_length)
nx=x+vx*stepny=y+vy*stepifdasher.is_current_dash():
draw.line((x, y, nx, ny), fill=lc, width=lw)
x=nxy=nyrem_length-=step
It strikes me now that it would be possible to add support for dashed lines in the Python layer of Pillow. Is that a contribution that would be interesting?
I can think of a solution to this when the width is 1 pixel, but more than that could be complicated. Would you be happy with only supporting widths of 1 pixel?
Activity
radarhere commentedon Aug 2, 2025
Are you just talking about straight lines?
From that StackOverflow post, you're aware of how to do this in Python, but you're requesting it be implemented by us in C so that it will be faster?
larsga commentedon Aug 2, 2025
Yes. In my case I need it specifically in the
ImageDraw.polygonfunction, but I imagine if you do this it will be done for all straight lines?Yes, exactly.
larsga commentedon Aug 2, 2025
Not hard to find more examples of people wanting this: 1, 2, 3.
larsga commentedon Aug 2, 2025
SVG has a nice algorithm and way to specify the dashing.
radarhere commentedon Aug 5, 2025
I'm not convinced a need for this to be fast is so widespread - it looks like most of the requests that you've linked to result in answers for how to achieve this in Python, and not comments from people asking for more speed. If you wanted this implemented in Python, that would seem achievable. From #8976, I think you have an above average need for speed.
However, in C, we use a scanline algorithm - for polygons, we're not drawing each line one at a time like a person would, we're determining which pixels should be visible in each horizontal row. To start using dashed lines, I think we would have to take a new approach.
I'm not saying this is impossible, just that it would not be simple. If you are able to post some example code, and precisely specify how fast you need it to run, that would be helpful.
larsga commentedon Aug 5, 2025
I think that's fair to say. In fact, I have implemented this in Python now, and was surprised to find that performance was completely acceptable. So if you do implement this feature request you don't need to do it for me.
Still, I find it strange that people have to implement this themselves. To me it seems like it should be in the library, but whether you want to do something about that is of course up to you.
I did look at implementing this myself in C, and as far as I can tell the scanlines you're talking about is for filled polygons. For unfilled polygons the draws one line at a time. It was with a view to adding a C implementation of dashing here that I first prototyped it in Python.
In case it will be useful to someone, here's my Python implementation of the SVG algorithm:
larsga commentedon Aug 5, 2025
It strikes me now that it would be possible to add support for dashed lines in the Python layer of Pillow. Is that a contribution that would be interesting?
radarhere commentedon Aug 5, 2025
You're offering to create a PR? Sure, feel free.
Add dash parameter support for line, polygon, and rectangle drawing
radarhere commentedon Jun 6, 2026
#9490 is a work-in-progress for this.
I can think of a solution to this when the width is 1 pixel, but more than that could be complicated. Would you be happy with only supporting widths of 1 pixel?