📜  Python中的魔杖线()函数

📅  最后修改于: 2022-05-13 01:55:09.828000             🧑  作者: Mango

Python中的魔杖线()函数

line()是 wand.drawing 模块中的另一个绘图函数。顾名思义,line()函数用于在图像中绘制一条线。 line()函数只需要两个参数,它们是我们要绘制的线的起点和终点。

示例 #1:

Python3
wand.drawing.line(start, end)


Python3
# Import required objects from wand modules
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
 
# generate object for wand.drawing
with Drawing() as draw:
    # set stroke color
    draw.stroke_color = Color('green')
    # set width for stroke
    draw.stroke_width = 1
    draw.line(( 50, 50),  # Stating point
             ( 150, 150))  # Ending point
    with Image(width = 200,
               height = 200,
               background = Color('white')) as img:
        # draw shape on image using draw() function
        draw.draw(img)
        img.save(filename ='line.png')


输出 :

示例#2:在预先存在的图像上画一条线。
源图像:


Python3

# Import required objects from wand modules
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
 
# generate object for wand.drawing
with Drawing() as draw:
    # set stroke color
    draw.stroke_color = Color('white')
 
    # set width for stroke
    draw.stroke_width = 1
    with Image(filename = "gog.png") as img:
        draw.line((( img.height)/2, 0),  # Stating point
                   ( 0, (img.width)/2))  # Ending point
 
        # draw shape on image using draw() function
        draw.draw(img)
        img.save(filename ='line2.png')

输出 :