Image#

Availability (Key)#

macOS

GTK

Windows

iOS

Android

Web

Terminal

An image is graphical content of arbitrary size.

Usage#

from pathlib import Path
import toga

# Load an image in the same folder as the file that declares the App class
my_image = toga.Image("brutus.png")

# Load an image at an absolute path
my_image = toga.Image(Path.home() / "path" / "to" / "brutus.png")

# Create an image from raw data
with (Path.home() / "path" / "to" / "brutus.png").open("rb") as f:
    my_image = toga.Image(data=f.read())

Notes#

  • PNG and JPEG formats are guaranteed to be supported. Other formats are available on some platforms:

    • macOS: GIF, BMP, TIFF

    • GTK: BMP

    • Windows: GIF, BMP, TIFF

Reference#

class toga.Image(path=None, *, data=None)#

Create a new image.

An image must be provided either a path or data, but not both.

Parameters:
  • path (UnionType[str, None, Path]) – Path to the image to load. This can be specified as a string, or as a pathlib.Path object. The path can be an absolute file system path, or a path relative to the module that defines your Toga application class.

  • data (Optional[bytes]) – A bytes object with the contents of an image in a supported format.

Raises:
  • FileNotFoundError – If a path is provided, but that path does not exist.

  • ValueError – If the path or data cannot be loaded as an image.

property data: bytes#

The raw data for the image, in PNG format.

Returns:

The raw image data in PNG format.

property height: int#

The height of the image, in pixels.

save(path)#

Save image to given path.

The file format of the saved image will be determined by the extension of the filename provided (e.g path/to/mypicture.png will save a PNG file).

Parameters:

path (str | Path) – Path where to save the image.

property size: int, int#

The size of the image, as a tuple

property width: int#

The width of the image, in pixels.