Open In App

PyCairo – How we can copy flattened path?

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

In this article we will learn how we can copy flattened path of object using PyCairo in python. This function is like copy_path() method except that any curves in the path will be approximated with piecewise-linear approximations.

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_flat() method with the Context object

Syntax : context.copy_path_flat()

Argument : It takes no arguments

Return : It returns path with piecewise-linear approximations

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 flattened copy of the current path
    c = context.copy_path_flat()
 
    # 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
line_to 339.800781 63.953125
line_to 339.218750 67.839844
line_to 338.261719 71.632813
line_to 336.949219 75.312500
line_to 335.277344 78.835938
line_to 333.269531 82.199219
line_to 330.929688 85.355469
line_to 328.273438 88.292969
line_to 325.332031 90.945313
line_to 322.175781 93.285156
line_to 318.812500 95.289063
line_to 315.285156 96.960938
line_to 311.609375 98.273438
line_to 307.816406 99.226563
line_to 303.929688 99.800781
line_to 299.976563 100.000000
line_to 296.011719 99.796875
line_to 292.125000 99.214844
line_to 288.332031 98.253906
line_to 284.660156 96.941406
line_to 281.128906 95.265625
line_to 277.773438 93.253906
line_to 274.617188 90.914063
line_to 271.687500 88.257813
line_to 269.027344 85.316406
line_to 266.691406 82.152344
line_to 264.683594 78.789063
line_to 263.023438 75.261719
line_to 261.710938 71.582031
line_to 260.761719 67.789063
line_to 260.187500 63.902344
line_to 260.000000 59.949219
close path
move_to 340.000000 60.000000

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads