Image#
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
ordata
, but not both.- Parameters:
path (
UnionType
[str
,None
,Path
]) – Path to the image to load. This can be specified as a string, or as apathlib.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.
- 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).