来自PIL文档:
PIL.ImageDraw.Draw.line(xy,fill = None,width = 0)
在xy列表中的坐标之间画一条线。
参数:
- xy - 像[(x,y),(x,y),...]这样的2元组序列或像[x,y,x,y,...]这样的数值。
- fill - 用于线条的颜色。
- width - 线宽,以像素为单位。 请注意,线条连接处理不当,因此宽折线看起来不太好。
我正在寻找解决此问题的方法。对我来说一个很好的解决方案是让PIL.ImageDraw
绘制的线具有圆形末端(capstyle
中的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的结果:
答案 0 :(得分:1)
我和你有同样的问题。但是,您可以通过简单地绘制与每个顶点处的线宽相同直径的圆来轻松解决问题。以下是您的代码,稍作修改,以解决问题
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()