gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:40:57 26 4
gpt4 key购买 nike

来自 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 绘制的线有圆角(TKinter 中的 capstyle)。 PIL.ImageDraw 中是否有等效项?

这是我想要得到的: enter image description here

最小工作示例:

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 的结果:

enter image description here

最佳答案

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()

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

结果:

Fixed line with joint='curve'

关于python - 用宽线修复 PIL.ImageDraw.Draw.line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com