Saturday, August 30, 2025
HomeLanguagesPyCairo – Drawing different type of line Joins

PyCairo – Drawing different type of line Joins

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
    #using 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
    #using 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
    #using 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
    #using 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:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7013 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS