如何为具有x,y坐标的多边形绘制圆角边?

2024-09-27 21:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从X,Y坐标绘制多边形,但对于圆角,我有X,Y点

我的代码如下,但是如果有其他库,我可以使用它

这里是我的输出图像:

Test image

这就是代码

def create_mask(dirct,filename,alistofpoint,height,width):
    myimg = np.zeros((height,width), dtype = "uint8")
    po = np.array(alistofpoint, np.int32)
    myimg_mod=cv2.fillPoly(myimg, [po],(255,255))
    cv2.imwrite(dirct+"//"+filename, myimg_mod)

Tags: 代码图像modnp绘制filename多边形width
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:38

您可以使用Pillow^{}模块,首先使用坐标绘制多边形,然后使用带圆角的宽线勾勒多边形轮廓

from operator import itemgetter
from pathlib import Path
from PIL import Image, ImageDraw


def create_mask(directory, filename, alistofpoint, height, width):
    mask = Image.new('L', (width, height), color=0)
    draw = ImageDraw.Draw(mask)
    draw.polygon(points, fill=255, outline=255)
    draw.line(points, fill=255, width=5, joint='curve')
    mask.save(Path(directory)/filename)
    return mask


pad = 25
points = [(114, 197), (96, 77), (110, 42), (138, 56,), (166, 124), (166, 174), (154, 195)]
width = max(points, key=itemgetter(0))[0] + pad
height = max(points, key=itemgetter(1))[1] + pad

mask = create_mask('\.', 'output_image.png', points, height, width)
mask.show()

这是一个3倍放大图,显示了左侧没有轮廓的多边形,右侧绘制了轮廓。在这样相对较低的图像分辨率下,效果有点难以看到

screenshot show effect of drawing outline

相关问题 更多 >

    热门问题