📜  PyCairo – 绘制不同类型的线帽

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

PyCairo – 绘制不同类型的线帽

在本文中,我们将学习如何在Python中使用 PyCairo 绘制不同的线帽类型。线帽是线的端点。

实施步骤:

  1. 导入 Pycairo 模块。
  2. 创建一个 SVG 表面对象并向其添加上下文。
  3. 设置上下文的颜色和线宽
  4. 使用 set_line_cap( ) 设置线帽样式
  5. 创建一条线。

PyCairo 中有三种不同类型的线帽样式。

1. set_line_cap ( cairo.LINE_CAP_BUTT )

例子 :

Python3
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    context.set_line_cap(cairo.LINE_CAP_BUTT)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    context.set_line_cap(cairo.LINE_CAP_ROUND)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")


Python
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_ROUND)
    context.move_to(30, 90)
    context.line_to(150, 90)
    context.stroke()
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 130)
    context.line_to(150, 130)
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")


输出 :

2. set_line_cap ( cairo.LINE_CAP_ROUND )

例子 :

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    context.set_line_cap(cairo.LINE_CAP_ROUND)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")

输出 :

3. set_line_cap ( cairo.LINE_CAP_SQUARE )

例子 :

Python

# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")

输出:

注意:使用 cairo.LINE_CAP_SQUARE 上限的线与使用 cairo.LINE_CAP_BUTT 上限的线的大小不同。如果线的宽度为 z 个单位,则带有 cairo.LINE_CAP_SQUARE 上限的线的尺寸正好是 z 个单位;开头为 z/2 个单位,结尾为 z/2 个单位。

在下面的Python示例中可以看到所有三种类型的行帽,并且还可以比较每个行帽的输出

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface
# here geek94 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek94.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 50)
    context.line_to(150, 50)
  
    # stroke out the color and width property
    context.stroke()
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_ROUND)
    context.move_to(30, 90)
    context.line_to(150, 90)
    context.stroke()
  
    # Setting line cap style
    context.set_line_cap(cairo.LINE_CAP_SQUARE)
    context.move_to(30, 130)
    context.line_to(150, 130)
    context.stroke()
  
    # printing message when file is saved
    print("File Saved")

输出 :