Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PyCairo – Radial gradients

  • Last Updated : 12 Nov, 2020

In the article we will learn to draw Radial gradients by python using 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.

A radial gradient is defined by a center point, an ending shape, and two or more color-stop points. 

 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

Steps of Implementation :



  1.  Import the PyCairo module.
  2.  Create a SVG surface object and add context to it.
  3.  Creating a Radial gradient object.
  4.  Defining loops or Condition, for adding color stripes
  5.  Creating a shape
  6.  The source is used to fill the interior of a rectangle by calling the fill ( ) method.

Example 1:

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)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    # Translate the context
    context.translate(60, 60)
  
    # Creating a Radial gradient object.
    r1 = cairo.RadialGradient(30, 30, 10, 30, 30, 90)
    r1.add_color_stop_rgba(0, 1, 1, 1, 1)
    r1.add_color_stop_rgba(1, 0.6, 0.6, 0.6, 1)
    context.set_source(r1)
  
    # Creating Circle
    context.arc(0, 0, 40, 0, 3.14 * 2)
  
    # Fill the color inside the Circle
    context.fill()
  
  # printing message when file is saved
print("File Saved")

Output :

Example 2 :

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)
    context.set_source_rgba(0, 0, 0, 1)
    context.set_line_width(12)
  
    # Translate the context
    context.translate(60, 60)
  
    # Creating a Radial gradient object.
    r2 = cairo.RadialGradient(0, 0, 10, 0, 0, 40)
    r2.add_color_stop_rgb(0, 1, 1, 0)
    r2.add_color_stop_rgb(0.8, 0, 0, 0)
    context.set_source(r2)
  
    # Creating Circle
    context.arc(0, 0, 40, 0, 3.14 * 2)
  
    # Fill the color inside the Circle
    context.fill()
  
  # printing message when file is saved
print("File Saved")

Output :




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!