In the article we will learn to draw Liner 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. The gradient line is defined by the center of the box containing the gradient image and by an angle. The colors of the gradient are determined by two or more points: the starting point, the ending point, and, in between, optional color-stop points.
Linear gradients are blendings of colours or shades of colours along a line. They are represented by a cairo.LinearGradient class in PyCairo.
Steps of Implementation :
- Import the PyCairo module.
- Create a SVG surface object and add context to it.
- Creating a liner 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 :
Example 2 :
Example 3 :
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.