---
title: "Image"
---
import XRef from "../../../../components/XRef.astro";
import Signature from "../../../../components/Signature.astro";

```python
from slint import Image
```

Image objects can be set on Slint Image elements for display. Construct Image objects from a path to an
image file on disk, using <XRef to="Image.load_from_path" />.

Images implement the [Buffer Protocol](https://docs.python.org/3/c-api/buffer.html).
To pass an image to third-party modules such as NumPy or Pillow, read its pixels through a buffer.
A buffer gives you read-only access to the pixels as an array of unsigned bytes with the shape `(height, width, 4)`.
Each pixel is encoded as RGBA, without premultiplied alpha.
The buffers of an image share their memory, and stay valid after the image is gone.
If the pixels can't be obtained, the image raises `BufferError`.

Example of saving an image with Pillow:
```python
import slint
from PIL import Image
import numpy as np

img = slint.Image.load_from_path("hello.jpeg")
Image.fromarray(np.asarray(img)).save("hello.png")
```

## Properties

### size

<Signature symbol="slint.slint.Image.size">size: <XRef to="tuple" plain />[<XRef to="int" plain />, <XRef to="int" plain />]</Signature>

The size of the image as a tuple of `width` and `height`.

### width

<Signature symbol="slint.slint.Image.width">width: <XRef to="int" plain /></Signature>

The width of the image in pixels.

### height

<Signature symbol="slint.slint.Image.height">height: <XRef to="int" plain /></Signature>

The height of the image in pixels.

### path

<Signature symbol="slint.slint.Image.path">path: pathlib.<XRef to="pathlib.Path" plain /> | None</Signature>

The path of the image if it was loaded from disk, or <XRef to="None" />.

## Methods

### load_from_path

<Signature symbol="slint.slint.Image.load_from_path">load_from_path(path: <XRef to="str" plain /> | os.<XRef to="os.PathLike" plain />[<XRef to="typing.Any" plain />] | pathlib.<XRef to="pathlib.Path" plain />) -&gt; <XRef to="slint.slint.Image" plain /></Signature>

Loads the image from the specified path. Raises `RuntimeError` if the image can't be loaded.

### load_from_svg_data

<Signature symbol="slint.slint.Image.load_from_svg_data">load_from_svg_data(data: <XRef to="typing.Sequence" plain />[<XRef to="int" plain />]) -&gt; <XRef to="slint.slint.Image" plain /></Signature>

Creates a new image from bytes that describe the image in SVG format.

### load_from_array

<Signature symbol="slint.slint.Image.load_from_array">load_from_array(array: <XRef to="collections.abc.Buffer" plain />) -&gt; <XRef to="slint.slint.Image" plain /></Signature>

Creates a new image from an array-like object that implements the [Buffer Protocol](https://docs.python.org/3/c-api/buffer.html).
Use this function to import images created by third-party modules such as matplotlib or Pillow.

The array must satisfy certain constraints to represent an image:

 - The buffer's format needs to be `B` (unsigned char)
 - The shape must be a tuple of (height, width, bytes-per-pixel)
 - If a stride is defined, the row stride must be equal to width * bytes-per-pixel, and the column stride must equal the bytes-per-pixel.
 - A value of 3 for bytes-per-pixel is interpreted as an RGB image, a value of 4 means RGBA.

The image is created by performing a deep copy of the array's data. Subsequent changes to the buffer aren't
reflected in a previously created Image.

Example of importing a Matplotlib figure into an image:
```python
import slint
import matplotlib

from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure

fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot()
ax.plot([1, 2, 3])
canvas.draw()

buffer = canvas.buffer_rgba()
img = slint.Image.load_from_array(buffer)
```

Example of loading an image with Pillow:
```python
import slint
from PIL import Image
import numpy as np

pil_img = Image.open("hello.jpeg")
array = np.array(pil_img)
img = slint.Image.load_from_array(array)
```