I want to draw a polygon as an outline and fill it with a specific color. This should be an easy enough task for PIL
... Unfortunately the fill
argument produces an image that bleeds out of the outline... Demonstration:
Step 1, lets show clearly what my outline should look like:
from PIL import Image, ImageDrawvertices = [(44, 124), (50, 48), (74, 46), (73, 123), (44, 124)]# draw as an outlineimage = Image.new('RGB', (150, 150), 'black')draw = ImageDraw.Draw(image)draw.line(vertices, fill='blue', width=1)image.save(r'outline.png')
This produces the image below:
This is great, and i can confirm in Photoshop that my corners corresponds to the specified coordinates. Now step 2, lets do the same using the polygon
function and draw a filled polygon:
# draw as a filled polygonimage = Image.new('RGB', (150, 150), 'black')draw = ImageDraw.Draw(image)draw.polygon(vertices, fill='white', outline='blue', width=1)image.save(r'filled.png')
Notice the two pixels appearing on this shape (highlighted below)
This is not acceptable. The filled polygon should no draw beyond the outline. Here is the desired output I would have expected (mocked up in Photoshop):
QUESTION
At the end of the day, I want to write a function which will take n
vertices to draw a clean polygon with a filled boundary the does not exceed the outline. Also, for technical reasons i cannot use matplotlib
, which means i cannot use skimage.draw.polygon_perimeter
but any other package (especially if leveraging numpy
) would be best