如何在PIL ImageDraw中增加多边形的厚度

10

我正在使用PIL工具,已经在图片上画了贝塞尔曲线,想要增加曲线的粗细。以下是我的代码:

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')

在多边形之后,您可能需要添加一个明确的 curve_draw.line(points,width=9) - meuh
1个回答

15

draw.polygon() 函数不能像 line() 一样接受 'width' 参数。

此外,line() 会接受一系列的点并绘制一条折线。

线的端点可能不太美观,但通过在端点处画圆圈,您可以让它们变得漂亮!

下面的代码绘制了一个美丽的粗红色多边形。

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")

1
新的 joints="curve" 参数将处理关节(除了最后一个,因此请扩展点列表以包括末尾的第二个条目)。 - Oddthinking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接