如何在 PIL 中使线对称?

How to make lines symmetrical in PIL?

我一直在研究这些线条,我想像图片中那样创建它,但是我变得很复杂。如何获得下图所示的线对称?

这是我到目前为止尝试过的,不多:/

from PIL import Image, ImageDraw

img = Image.new('RGB', (1000, 1000), (255, 255, 255))
draw = ImageDraw.Draw(img)

for y in range (-4000, 2000, 200):
    draw.line(((y, img.size[0]+img.size[0]), 
               (img.size[1]+img.size[1]+img.size[1], y)), (0, 0, 0), 20)

for x in range (-2000, 1000, 100):
    draw.line(((x,-x),(x+img.size[0], -x+img.size[1]+img.size[1])), (0, 0, 0), 20)

我想创建这样的线条...

我认为你创建的计算过于复杂,这造成了问题

对称线应该有相似的计算,但符号不同。

两者都应该有 y0height

from PIL import Image, ImageDraw

img = Image.new('RGB', (1000, 1000), (255, 255, 255))
draw = ImageDraw.Draw(img)

w = img.size[0]
h = img.size[1]

offset = 400
step = 100

for x in range(-offset, w+offset, step):
    draw.line(((x, 0), (x+offset, h)), (0, 0, 0), 20)
    draw.line(((x, 0), (x-offset, h)), (0, 0, 0), 20)
    
img.show()

给予

而对于 offset = image_width (offset = 1000) 它给出矩形


编辑:

使用move_xmove_y移动行的版本。

因为移动行后它显示行尾所以我使用 line_width 在 window 之外开始和结束行。

from PIL import Image, ImageDraw

img = Image.new('RGB', (1000, 1000), (0, 200, 0))
draw = ImageDraw.Draw(img)

w = img.size[0]
h = img.size[1]

line_width = 10 # 

move_x = 10
move_y = 50

offset = w # 400
step = 200

for x in range(-offset, w+offset, step):
    x1 = x  + move_x - move_y - line_width
    x2 = x1 + offset          + line_width*2
    
    x3 = x  + move_x + move_y + line_width
    x4 = x3 - offset          - line_width*2
    
    draw.line(((x1, 0-line_width), (x2, h+line_width)), (0, 0, 0), line_width)
    draw.line(((x3, 0-line_width), (x4, h+line_width)), (0, 0, 0), line_width)

img.show()
img.save('output.png')