用宽线修复 PIL.ImageDraw.Draw.line

Fix PIL.ImageDraw.Draw.line with wide lines

来自 PIL 文档:

PIL.ImageDraw.Draw.line(xy, fill=None, width=0)

Draws a line between the coordinates in the xy list.

Parameters:

  • xy – Sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...].
  • fill – Color to use for the line.
  • width – The line width, in pixels. Note that line joins are not handled well, so wide polylines will not look good.

我正在寻找解决此问题的方法。对我来说,一个好的解决方案是让 PIL.ImageDraw 绘制的线有圆角(capstyle in TKinter)。 PIL.ImageDraw中是否有等价物?

这是我想要得到的:

最小工作示例:

from PIL import Image, ImageDraw

WHITE = (255, 255, 255)
BLUE = "#0000ff"
MyImage = Image.new('RGB', (600, 400), WHITE)
MyDraw = ImageDraw.Draw(MyImage)

MyDraw.line([100,100,150,200], width=40, fill=BLUE)
MyDraw.line([150,200,300,100], width=40, fill=BLUE)
MyDraw.line([300,100,500,300], width=40, fill=BLUE)

MyImage.show()

MWE 的结果:

我和你有同样的问题。但是,您可以通过简单地绘制一个直径与每个顶点处的线宽相同的圆来轻松解决问题。下面是你的代码,稍作修改,以解决问题

from PIL import Image, ImageDraw

WHITE = (255, 255, 255)
BLUE = "#0000ff"
RED  = "#ff0000"
MyImage = Image.new('RGB', (600, 400), WHITE)
MyDraw = ImageDraw.Draw(MyImage)

# Note: Odd line widths work better for this algorithm,
# even though the effect might not be noticeable at larger line widths

LineWidth = 41

MyDraw.line([100,100,150,200], width=LineWidth, fill=BLUE)
MyDraw.line([150,200,300,100], width=LineWidth, fill=BLUE)
MyDraw.line([300,100,500,300], width=LineWidth, fill=BLUE)

Offset = (LineWidth-1)/2

# I have plotted the connecting circles in red, to show them better
# Even though they look smaller than they should be, they are not.
# Look at the diameter of the circle and the diameter of the lines -
# they are the same!

MyDraw.ellipse ((150-Offset,200-Offset,150+Offset,200+Offset), fill=RED)
MyDraw.ellipse ((300-Offset,100-Offset,300+Offset,100+Offset), fill=RED)

MyImage.show()

ImageDraw.line 的标准选项 joint='curve' 旨在修复它。

你的例子可能看起来像

from PIL import Image, ImageDraw

WHITE = (255, 255, 255)
BLUE = "#0000ff"
MyImage = Image.new('RGB', (600, 400), WHITE)
MyDraw = ImageDraw.Draw(MyImage)

line_points = [(100, 100), (150, 200), (300, 100), (500, 300)]
MyDraw.line(line_points, width=40, fill=BLUE, joint='curve')

MyImage.show()

需要特别注意处理端点,但关节是固定的。

结果: