在 ImageDraw 中绘制圆角线 python

Draw rounded corner line in ImageDraw python

如何在ImageDraw中绘制圆角线?

我可以用

画画
draw.line((x, y, x1, y1), width=4)

但是线条的角不是圆的,而是平直的。

PIL/Pillow 中的图形绘制基元非常基础,无法像 pycairo (tutorial and examples 等专用图形绘制包那样完成漂亮的斜角、米、抗锯齿和圆角边缘。

话虽如此,您可以通过在线段末端绘制圆圈来模拟在线段的圆边:

from PIL import Image, ImageDraw

im = Image.new("RGB", (640, 240))
dr = ImageDraw.Draw(im)

def circle(draw, center, radius, fill):
    dr.ellipse((center[0] - radius + 1, center[1] - radius + 1, center[0] + radius - 1, center[1] + radius - 1), fill=fill, outline=None)

W = 40
COLOR = (255, 255, 255)

coords = (40, 40, 600, 200)

dr.line(coords, width=W, fill=COLOR)
circle(dr, (coords[0], coords[1]), W / 2, COLOR)
circle(dr, (coords[2], coords[3]), W / 2, COLOR)

im.show()