python - 如何为具有 x,y 坐标的多边形绘制圆边?
问题描述
我需要从 X、Y 坐标绘制多边形,但圆角我有 X、Y 的点
我的代码在下面,但是如果有另一个库,我可以使用它。
这是我的输出图像:
这是代码
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)
解决方案
您可以通过使用Pillow的ImageDraw
模块来做到这一点,首先使用坐标绘制多边形,然后用圆角的宽线勾勒出它的轮廓。
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()
这是一个 3X 放大图,显示了左侧没有轮廓的多边形,右侧绘制了它。在如此相对较低的图像分辨率下,效果有点难以看到。
推荐阅读
- elasticsearch - 有什么方法可以通知 Nifi 处理器
- c# - c#visual studio - winforms 在运行时没有正确显示
- reactjs - 能反应原生注入gatsbyjs吗?
- r - 将 tibble 行作为未列出的命名向量传递给命名函数,包括列表列
- c++ - 了解如何从 RFID Arduino 读取 UID
- url - 如何计算破折号媒体片段名称的 $Time$ 变量?
- flutter - Json.Encode() 不识别对象,将它们设置为字符串 FLUTTER
- elasticsearch - Elasticsearch:按最多的应该匹配进行排名
- java - 将重复项转换为字符串中的不同字母
- java - 是否可以在左侧和右侧创建一个带有类的面板?