Open In App

PyCairo – Drawing Bezier curve

Last Updated : 23 Jan, 2022
Like Article

In this article we will learn how we can draw a simple bezier curve using PyCairo in python. A Bezier curve is a versatile mathematical curve that can be used to create a wide variety of different shapes in vector graphics. 

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 curve using curve_to ( )
  4. Setting context color and width

Below is the Implementation :


# importing pycairo
import cairo
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
    # creating a cairo context object for SVG surface
    # using Context method
    context = cairo.Context(surface)
    # move the context to x,y position
    context.move_to(50, 200)
    # Drawing Curve
    context.curve_to(150, 75, 225, 50, 350, 150)
    # setting color of the context
    context.set_source_rgb(1, 0, 0)
    # setting width of the context
    # stroke out the color and width property
# printing message when file is saved
print("File Saved")

Output :

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads