Skip to content
Related Articles

Related Articles

PyCairo – How we can get clip extents ?
  • Last Updated : 26 Nov, 2020

In this article, we will learn how we can get clip extents using PyCairo in python. This module computes a bounding box in user coordinates covering the area inside the current clip. The current clip masks out any changes to the surface.

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 stands for Scalable Vector Graphics. It basically defines vector-based graphics in XML format. SVG graphics do NOT lose any quality if they are zoomed or resized. Every element and every attribute in SVG files can be animated.

In order to this, we will use the clip_extents() method with the Context object

Syntax : context.clip_extents()



Argument : It takes no arguments

Return : (x1, y1, x2, y2), all float

Example :

Python3




# importing pycairo
import cairo
  
# creating a SVG surface
# here geek1 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek1.svg", 700, 700) as surface:
  
    # creating a cairo context object
    context = cairo.Context(surface)
  
    # creating a arc without using closing path method
    context.arc(100, 60, 40, 0, 1*22/7)
  
    # stroke the context to remove the moved pen
    context.stroke()
  
    # creating a arc with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
  
    # makeing close path
    context.close_path()
  
    # getting clip extents
    a = context.clip_extents()
  
    # stroke the context to remove the moved pen
    context.stroke()
  
# printing message when file is saved
print(a)

Output :

(0.0, 0.0, 700.0, 700.0)

 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :