imagepython-2.7python-imaging-librarypolygonsar

How to color the mask image obtained out of a polygon


I am getting a mask out of a polygon and below is the image. Now I see that there is a white boundary but I want not just the boundary but inside boundary as white too.

enter image description here

Here is my code:

sar_polygon = Image.new('L', (int(range_samples), int(azimuth_lines)), 0) 
        draw = ImageDraw.Draw(sar_polygon)
        for vertex in range(len(sar_ver)):
            st = sar_ver[vertex]
            try:
                end = sar_ver[vertex + 1]
            except IndexError:
                end = sar_ver[0]
            draw.line((st[0], st[1], end[0], end[1]), fill=1)

sar_polygon.save('polygon.jpg', 'JPEG')

Solution

  • You are currently drawing lines along the edges. You are interested in the polygon method, or perhaps the rectangle method - http://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.ImageDraw.polygon.

    from PIL import Image, ImageDraw
    sar_polygon = Image.new('RGB', (500, 500)) 
    draw = ImageDraw.Draw(sar_polygon)
    sar_ver = ((100,100),(200,100),(200,200),(100,200))
    draw.polygon(sar_ver, fill='#f00')
    sar_polygon.save('polygon.jpg', 'JPEG')