PyCairo – Drawing the Roundrect
Last Updated :
23 Jan, 2022
In this article, we will learn how we can draw a simple Roundrect shape using PyCairo in python. A roundrect is a rectangle with rounded corners
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 :
- Import the Pycairo module.
- Create an SVG surface object and add context to it.
- Creating a function to make roundrect shape
- Calling the function with parameters
- Setting color of the context & line width
Below is the Implementation :
Python3
import cairo
import math
def roundrect(context, x, y, width, height, r):
context.arc(x + r, y + r, r,
math.pi, 3 * math.pi / 2 )
context.arc(x + width - r, y + r, r,
3 * math.pi / 2 , 0 )
context.arc(x + width - r, y + height - r,
r, 0 , math.pi / 2 )
context.arc(x + r, y + height - r, r,
math.pi / 2 , math.pi)
context.close_path()
with cairo.SVGSurface( "geek95.svg" , 700 , 700 ) as surface:
context = cairo.Context(surface)
context.set_line_width( 10 )
context.set_source_rgb( 0 , 0 , 0.5 )
roundrect(context, 100 , 100 , 400 , 200 , 50 )
context.stroke()
print ( "File Saved" )
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...