Skip to content
Related Articles
Open in App
Not now

Related Articles

PyCairo – How to close the path?

Improve Article
Save Article
  • Last Updated : 23 Jan, 2022
Improve Article
Save Article

In this article we will learn how we can make our pen path close using PyCairo in python. Adds a line segment to the path from the current point to the beginning of the current sub-path.

Note : As of cairo version 1.2.4 any call to close_path() will place an explicit MOVE_TO element into the path immediately after the CLOSE_PATH element,

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.  

SVG file is a graphics file that uses a two-dimensional vector graphic format created by the World Wide Web Consortium (W3C). It describes images using a text format that is based on XML. SVG files are developed as a standard format for displaying vector graphics on the web.

Steps of Implementation :

  1. Import the Pycairo module.
  2. Create a SVG surface object and add context to it.
  3. Creating any object like arc, curve etc.
  4. Now we can close the arc to make semi-circle by close_path() method.

Below is the Implementation :


# importing pycairo
import cairo
# creating a SVG surface
# here geek1 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek1.svg", 700, 700) as surface:
    # creating a cairo context object
    context = cairo.Context(surface)
    # creating a arc without using closing path method
    context.arc(100, 60, 40, 0, 1*22/7)
    # stroke the context to remove the moved pen
    # creating a arc with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
    # making close path
    # stroke the context to remove the moved pen
# printing message when file is saved
print("File Saved")

Output :

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!