Polygon with holes in aggdraw

Posted by Johan on Tuesday, March 6, 2012

Drawing polygons with holes have haunted me for a while and recently I had to do it in Python with PIL.

Googling the subject gives you a hint.

This code:
import os import Image import aggdraw

draw = aggdraw.Draw('RGB', (100, 100), 'white')

path = aggdraw.Path()
path.moveto(10, 10)
path.lineto(10,60,60,60)
path.lineto(60,10)
path.lineto(10,10)
path.moveto(20,20)
path.lineto(40,20)
path.lineto(40,40)
path.lineto(20,40)
path.lineto(20,20)

pen = aggdraw.Brush("black")
draw.path((25, 25), path, pen, None)

img = Image.fromstring('RGB', (100, 100), draw.tostring())
p = os.path.join(os.path.dirname(__file__), 'box.png')
img.save(p)

 Gives the following image:

Polygon with hole created with aggdraw