Saving / Displaying Images
- draw
- draw_image
- show
- save_png
- save_pdf
- save_svg
- encode_as_png
- encode_as_jpeg
- array
- make_raster_surface
draw
draw(clear_buffer: bool = False) -> 'Gw'
Draw the visualisation to the raster surface. Caches state for using with interactive functions.
Creates the raster surface if it doesn’t exist yet.
Parameters:
clear_buffer
(bool): Clears any buffered reads before re-drawing
Returns:
Gw
: Self for method chaining
draw_image
draw_image() -> Image.Image
Draw the visualisation and return it as a PIL Image.
Returns:
PIL.Image
: The visualisation as a PIL Image
Raises:
ImportError
: If Pillow could not be imported
Example:
# Get the visualisation as a PIL Image
img = gw.draw_image()
img.save("output.png")
show
show() -> None
Convenience method for showing the image on screen. Equivalent to gw.draw_image().show()
Raises:
ImportError
: If Pillow could not be imported
save_png
save_png(path: str) -> 'Gw'
Draws and saves the raster canvas to a PNG file.
Parameters:
path
(str): Path to save the PNG file
Returns:
Gw
: Self for method chaining
Example:
gw.save_png("visualisation.png")
save_pdf
save_pdf(path: str) -> 'Gw'
Draws and saves a PDF file using the current configuration.
Parameters:
path
(str): Path to save the PDF file
Returns:
Gw
: Self for method chaining
Example:
gw.save_pdf("visualization.pdf")
save_svg
save_svg(path: str) -> 'Gw'
Saves an SVG file using the current configuration.
Parameters:
path
(str): Path to save the SVG file
Returns:
Gw
: Self for method chaining
Example:
gw.save_svg("visualization.svg")
encode_as_png
encode_as_png(compression_level: int = 6) -> Optional[bytes]
Encode the current canvas as PNG and return the binary data.
Parameters:
compression_level
(int): PNG compression level (0-9)
Returns:
bytes
orNone
: PNG encoded image data or None if the raster surface hasn’t been created
encode_as_jpeg
encode_as_jpeg(quality: int = 80) -> Optional[bytes]
Encode the current canvas as JPEG and return the binary data.
Parameters:
quality
(int): JPEG quality (0-100)
Returns:
bytes
orNone
: JPEG encoded image data or None if the raster surface hasn’t been created
Raises:
RuntimeError
: If image encoding failed
array
array() -> Optional[np.ndarray]
Convert the pixel data to a numpy array using zero-copy interface.
Returns:
numpy.ndarray
orNone
: RGBA image data as a 3D numpy array (height × width × 4) or None if the raster surface hasn’t been created
Example:
# Get the visualization as a numpy array
img_array = gw.draw().array()
make_raster_surface
make_raster_surface(width: int = -1, height: int = -1) -> 'Gw'
Create a raster surface for rendering. This is usually called automatically by drawing functions if needed, but can be called explicitly to pre-allocate the rendering surface.
Parameters:
width
(int, optional): Width of the raster surface. If -1, uses the current canvas width.height
(int, optional): Height of the raster surface. If -1, uses the current canvas height.
Returns:
Gw
: Self for method chaining
Raises:
RuntimeError
: If the raster surface could not be created
Example:
# Explicitly create a raster surface
gw.make_raster_surface(1200, 800)