Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase thickness of polygon in PIL ImageDraw

Iam working with PIL, I have drawn bezier curve on image, I want to increase thickness of that curve. Here is my code:

for image in images:
    img = Image.open("/home/ec2-user/virtualenvs/axonator-production/axonator/media/app_data/ax_picture_20150831_213704.png").convert('RGBA')
    for annotation in image["annotations"]:
        xys = []
        frame = annotation["frame"].split(",")
        frame = [int(float(frame[0])),int(float(frame[1])),int(float(frame[2])),int(float(frame[3]))]
        frame_location = (frame[0],frame[1])
        frame_size = (5000 , 5000)
        for point in annotation["path"]:
            pt = point["points"].split(",")
            xys.append((pt[0],pt[1]))
        bezier = make_bezier(xys)
        points = bezier(ts)
        curve = Image.new('RGBA', frame_size)
        import pdb; pdb.set_trace()
        curve_draw = ImageDraw.Draw(curve)
        curve_draw.polygon(points,outline="red")
        curve_draw.text(points[0],str(order))
        order = order + 1
        img.paste(curve,frame_location,mask = curve)
    img.save('out.png')
like image 217
Gaurav Bagul Avatar asked Oct 17 '15 14:10

Gaurav Bagul


1 Answers

The function draw.polygon() can't take a 'width' argument like line() can.

Besides that, line() will take a sequence of points and will draw a polyline.

The line endings will be ugly, but by drawing circles on the endings, you can make them pretty!

The code below draws a beautiful thick red polygon.

enter image description here

from PIL import Image, ImageDraw

points = (
    (30, 40),
    (120, 60),
    (110, 90),
    (20, 110),
    (30, 40),
    )

im = Image.new("RGB", (130, 120))
dr = ImageDraw.Draw(im)
dr.line(points, fill="red", width=9)
for point in points:
    dr.ellipse((point[0] - 4, point[1] - 4, point[0]  + 4, point[1] + 4), fill="red")
im.save("polygon.png")
like image 55
Michiel Overtoom Avatar answered Nov 02 '22 23:11

Michiel Overtoom