📜  PyCairo - 绘制不同类型的线连接

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

PyCairo - 绘制不同类型的线连接

在本文中,我们将学习如何在Python中使用 PyCairo 使用不同的连接样式(如斜角、圆角和斜接)连接线。

Pycairo是一个Python模块,为 Cairo 图形库提供绑定。这个库用于在Python中创建 SVG 即矢量文件。打开 SVG 文件以查看它(只读)的最简单快捷的方法是使用现代网络浏览器,如 Chrome、Firefox、Edge 或 Internet Explorer——几乎所有这些都应该为 SVG 格式提供某种渲染支持.

要安装此模块,请在终端中运行此命令:

pip install pycairo

方法:

  • 导入 Pycairo 模块。
  • 创建一个 SVG 表面对象并向其添加上下文。
  • 设置上下文的颜色和线宽
  • 创建矩形
  • 使用 set_line_join( ) 设置线连接样式

PyCairo 中的线连接样式有三种不同的方法。

方法#1:使用 set_line_join ( cairo.LINE_JOIN_MITER )。

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(14)
      
      
    context.rectangle(30, 30, 100, 100)   
      
    #Setting line join style
    context.set_line_join(cairo.LINE_JOIN_MITER)
      
    # stroke out the color and width property
    context.stroke()
      
    
    # printing message when file is saved
    print("File Saved")


Python3
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(14)
         
    context.rectangle(30, 30, 100, 100)        
      
    #Setting line join style
    context.set_line_join(cairo.LINE_JOIN_BEVEL)
      
    # 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(14)    
    context.rectangle(30, 30, 100, 100)        
      
    #Setting line join style
    context.set_line_join(cairo.LINE_JOIN_ROUND)
      
    # 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(14)    
    context.rectangle(30, 30, 100, 100)  
      
    # Setting line join style as Miter
    context.set_line_join(cairo.LINE_JOIN_MITER)
    # stroke out the color and width property
    context.stroke()
  
    context.rectangle(160, 30, 100, 100)
      
    # Setting line join style as Bevel
    context.set_line_join(cairo.LINE_JOIN_BEVEL)
    # stroke out the color and width property
    context.stroke()
    context.rectangle(100, 160, 100, 100)
      
    # Setting line join style as Round
    context.set_line_join(cairo.LINE_JOIN_ROUND)
    # stroke out the color and width property
    context.stroke()
        
    # printing message when file is saved
    print("File Saved")


输出:

方法#2:使用 set_line_join ( cairo.LINE_JOIN_BEVEL )。

例子

蟒蛇3

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(14)
         
    context.rectangle(30, 30, 100, 100)        
      
    #Setting line join style
    context.set_line_join(cairo.LINE_JOIN_BEVEL)
      
    # stroke out the color and width property
    context.stroke()
      
    # printing message when file is saved
    print("File Saved")

输出 :

方法 #3:使用 set_line_join ( cairo.LINE_JOIN_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(14)    
    context.rectangle(30, 30, 100, 100)        
      
    #Setting line join style
    context.set_line_join(cairo.LINE_JOIN_ROUND)
      
    # stroke out the color and width property
    context.stroke()
      
    # printing message when file is saved
    print("File Saved")

输出 :

所有三种类型的行连接都可以在下面的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(14)    
    context.rectangle(30, 30, 100, 100)  
      
    # Setting line join style as Miter
    context.set_line_join(cairo.LINE_JOIN_MITER)
    # stroke out the color and width property
    context.stroke()
  
    context.rectangle(160, 30, 100, 100)
      
    # Setting line join style as Bevel
    context.set_line_join(cairo.LINE_JOIN_BEVEL)
    # stroke out the color and width property
    context.stroke()
    context.rectangle(100, 160, 100, 100)
      
    # Setting line join style as Round
    context.set_line_join(cairo.LINE_JOIN_ROUND)
    # stroke out the color and width property
    context.stroke()
        
    # printing message when file is saved
    print("File Saved")

输出: