How to draw polygons with Python?

前端 未结 6 1002
南笙
南笙 2021-02-05 08:38

I have input values of x, y coordinates in the following format:

[[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]

I want to draw polygons, but I don\'t

6条回答
  •  时光取名叫无心
    2021-02-05 09:11

    Another way to draw a polygon is this:

    import PIL.ImageDraw as ImageDraw
    import PIL.Image as Image
    
    image = Image.new("RGB", (640, 480))
    
    draw = ImageDraw.Draw(image)
    
    # points = ((1,1), (2,1), (2,2), (1,2), (0.5,1.5))
    points = ((100, 100), (200, 100), (200, 200), (100, 200), (50, 150))
    draw.polygon((points), fill=200)
    
    image.show()
    

    Note that you need to install the pillow library. Also, I scaled up your coordinates by the factor of 100 so that we can see the polygon on the 640 x 480 screen.

    Hope this helps.

提交回复
热议问题