当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Wand line()用法及代码示例


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

用法:
wand.drawing.line(start, end)

参数:

参数 输入类型 描述
start 序列或(数字。整数,数字。整数) 表示弧的x和y的起点。
end 序列或(数字。整数,数字。整数) 表示圆弧的x和y的终点。

范例1:

# 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:在预先存在的图像上画一条线。

源图像:

# 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')

输出:





注:本文由纯净天空筛选整理自RahulSabharwal大神的英文原创作品 Wand line() function in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。