Related Articles

Related Articles

PyCairo – Drawing different type of line caps
  • Last Updated : 12 Nov, 2020

In this article, we will learn how we can draw different line caps types using PyCairo in python. The line caps are endpoints of lines.

Steps of Implementation :

  1. Import the Pycairo module.
  2. Create a SVG surface object and add context to it.
  3. Setting color of the context & line width
  4. Setting of line cap style using set_line_cap( )
  5. Creating a line.

There are three different types line cap styles in PyCairo.

1. set_line_cap ( cairo.LINE_CAP_BUTT )

Example :



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

2. set_line_cap ( cairo.LINE_CAP_ROUND )

Example :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



3. set_line_cap ( cairo.LINE_CAP_SQUARE )

Example :

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Note: A line with cairo.LINE_CAP_SQUARE cap used has a different size than a line with cairo.LINE_CAP_BUTT cap. If the line is z units wide, the line with cairo.LINE_CAP_SQUARE cap will be exactly z units greater in size; z/2 units at the beginning and z/2 units at the end.

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :