Skip to content
Related Articles

Related Articles

Improve Article

PyCairo – Drawing different type of line Joins

  • Last Updated : 12 Nov, 2020

In this article, we will learn how lines can be joined using different join styles like Bevel, Round, and Miter in python using PyCairo. 

Pycairo is a Python module providing bindings for the Cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read-only) is with a modern web browser like Chrome, Firefox, Edge, or Internet Explorer—nearly all of them should provide some sort of rendering support for the SVG format.

For install this module run this command into your terminal:

pip install pycairo

Approach:

  • Import the Pycairo module.
  • Create a SVG surface object and add context to it.
  • Setting color of the context & line width
  • Creating a rectangular shape
  • Setting of line joins style using set_line_join( )

There are three different Methods for line joins styles in PyCairo.



Method #1: With 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")

Output:

Method #2: With set_line_join ( cairo.LINE_JOIN_BEVEL ).

Example

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")

Output :



Method #3:  With set_line_join ( cairo.LINE_JOIN_ROUND )

Example :

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")

Output :

All three type of line joins can be seen in below python Example, and output of each line joins can also be compared 

Example :

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")

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :