Lulu
Lulu

Reputation: 175

Draw a polygon using draw.polygon

import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow 

#that s my class Point(2D)
class Pnt(namedtuple('Pnt', 'x y')):
    __slots__ = ()
    def __init__(self, *args):
        super(Pnt, self).__init__(*args)

Here is the vector of the vertices(convex polygon)

vertix = []
vertix.append(Pnt(50, 100))
vertix.append(Pnt(100, 200))
vertix.append(Pnt(200, 200))
vertix.append(Pnt(300, 0))
vertix.append(Pnt(250, -200))
vertix.append(Pnt(100, -100))

Here I want to draw the polygon. The problem is it is not centered, so almost half the polynom is outside the frame.

im = Image.new("RGB", (600,800))
draw = ImageDraw.Draw(im)
draw.polygon(vertix, fill=230, outline=255)
im.show()

Upvotes: 0

Views: 892

Answers (1)

PM 2Ring
PM 2Ring

Reputation: 55469

If you want to center your polygon in the Image you can

1) determine the bounding box of your polygon,

2) calculate the coordinates of the center of the bounding box,

3) calculate the vector required to translate the center of the bounding box to the center of the Image rectangle,

4) create a new polygon by translating the coords of the vertices of the old poly by the vector found in step 3.

Upvotes: 2

Related Questions