Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PyCairo – Drawing Bezier curve

  • Last Updated : 12 Nov, 2020

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.  

 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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 :

Python3




# 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
    # useing 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
    context.set_line_width(4)
    # stroke out the color and width property
    context.stroke()
  
# printing message when file is saved
print("File Saved")

Output :

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!