PyCairo – Drawing the outline

In this article, we will learn how we can draw an outline to a solid surface using PyCairo in python. The outline is basically a border to the object, it is used to highlight the object.
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.
 - Setting color of the context & line width
 - Creating Shape
 - Preserving inside color of object, so that the border color can be changed which can be used as outline
 - Setting color & width of border i.e outline
 
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)       # Creating shape     context.rectangle(25, 50, 50, 120)       # setting color of the context     context.set_source_rgb(1, 0, 0)       # Fill the color inside      context.fill()       # Creating shape     context.rectangle(125, 50, 50, 120)       # setting color of the context     context.set_source_rgb(0, 1, 1)       # Setting outline width     context.set_line_width(4)       # stroke out the color and width property     context.stroke()       # Creating shape     context.rectangle(225, 50, 50, 120)       # setting color of the context for inside     context.set_source_rgb(0, 0, 1)       # Preserving inside color of object     context.fill_preserve()       # setting color of the context for outline     context.set_source_rgb(1, 1, 0)       context.set_line_width(4)       # stroke out the color and width property     context.stroke()   # 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)       # Creating shape   context.arc(500, 60, 40, 0, 2*22/7)       # setting color of the context for inside   context.set_source_rgb(0, 0, 0)       # Preserving inside color of object   context.fill_preserve()       # setting color of the context for outline   context.set_source_rgb(1, 1, 0)       # Setting outline width   context.set_line_width(4)       # stroke out the color and width property   context.stroke()     # printing message when file is saved print("File Saved") | 
Output :
				
					



