PyCairo – Radial gradients
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.
Steps of Implementation :
- Import the PyCairo module.
- Create a SVG surface object and add context to it.
- Creating a Radial gradient object.
- Defining loops or Condition, for adding color stripes
- Creating a shape
- 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 # using 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 # using 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 :
Please Login to comment...