Skip to content
Related Articles

Related Articles

PyCairo – How we can get fill extents ?

Improve Article
Save Article
Like Article
  • Last Updated : 23 Jan, 2022

In this article we will learn how we can get fill extents using PyCairo in python. Computes a bounding box in user coordinates covering the area that would be affected, (the “colored” area), by a context.fill() operation given the current path and fill parameter. If the current path is empty, returns an empty rectangle (0, 0, 0, 0). Surface dimensions and clipping are not taken into account.

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.

Note that fill_extents() must necessarily do more work to compute the precise colored areas in light of the fill rule.

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

Syntax : context.fill_extents()

Argument : It takes no arguments

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

Examples 1 :

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 with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
 
    # making close path
    context.close_path()
    context.fill()
 
    # getting fill extends
    a = context.fill_extents()
 
    # stroke the context to remove the moved pen
    context.stroke()
 
# printing message when file is saved
print(a)

Output :

(0.0, 0.0, 0.0, 0.0)

Examples 2 :

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 with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
 
    # getting fill extends
    a = context.fill_extents()
 
    # making close path
    context.close_path()
    context.fill()
 
    # stroke the context to remove the moved pen
    context.stroke()
 
# printing message when file is saved
print(a)

Output :

(260.0, 59.94921875, 340.0, 100.0)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!