Open In App

PyCairo – How we can copy path?

Last Updated : 23 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will learn how we can copy path of object using PyCairo in python. Creates a copy of the current path and returns it to the user as a Path.

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.

In order to this we will use copy_path() method with the Context object

Syntax : context.copy_path()

Argument : It takes no arguments

Return : It returns path

Example :

Python3




# 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
    context.stroke()
 
    # creating a arc with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
 
    # making close path
    context.close_path()
 
    # Gets a copy of the current path
    c = context.copy_path()
 
    # stroke the context to remove the moved pen
    context.stroke()
 
# printing message when file is saved
print(c)


Output :

move_to 340.000000 60.000000 
curve_to 340.000000 70.613281 335.781250 80.792969 328.273438 88.292969 
curve_to 320.769531 95.796875 310.585938 100.007813 299.976563 100.000000 
curve_to 289.363281 99.992188 279.187500 95.769531 271.687500 88.257813 
curve_to 264.191406 80.746094 259.988281 70.562500 260.000000 59.949219 
close path 
move_to 340.000000 60.000000 
 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads