Skip to content

pytauri.image

tauri::image

Classes:

Name Description
Image

__all__ module-attribute

__all__ = ['Image']

Image

Bases: Image

tauri::image::Image

Methods:

Name Description
__new__
from_pil

Creates a new image using the provided PIL image.

from_bytes

Create an image from bytes.

from_path

Create an image from a file path.

Attributes:

Name Type Description
rgba bytes
width int
height int

rgba property

rgba: bytes

width property

width: int

height property

height: int

__new__

__new__(rgba: bytes, width: int, height: int) -> Self
Source code in python/pytauri/src/pytauri/ffi/image.py
def __new__(cls, rgba: bytes, width: int, height: int, /) -> Self: ...

from_pil classmethod

from_pil(image: Image) -> Self

Creates a new image using the provided PIL image.

The original tauri::image::Image::from_bytes only supports .ico and .png formats. But this method supports all formats supported by PIL.

Note

Tauri requires images to be in RGBA mode. If the provided image is not in RGBA mode, it will be converted to RGBA mode as a copy.

Source code in python/pytauri/src/pytauri/image.py
@classmethod
def from_pil(cls, image: PILImage.Image) -> Self:
    """Creates a new image using the provided `PIL` image.

    The original `tauri::image::Image::from_bytes` only supports `.ico` and `.png` formats.
    But this method supports **all formats supported by `PIL`**.

    !!! note
        `Tauri` requires images to be in `RGBA` mode.
        If the provided image is not in `RGBA` mode, it will be converted to `RGBA` mode as a copy.
    """
    if image.mode != cls._MODE:
        image = image.convert(cls._MODE)
    return cls(image.tobytes(), *image.size)

from_bytes classmethod

from_bytes(bytes_: Union[bytes, bytearray, memoryview]) -> Self

Create an image from bytes.

This method calls pytauri.image.Image.from_pil internally.

Source code in python/pytauri/src/pytauri/image.py
@classmethod
def from_bytes(cls, bytes_: Union[bytes, bytearray, memoryview], /) -> Self:
    """Create an image from bytes.

    This method calls [pytauri.image.Image.from_pil][] internally.
    """
    return cls.from_pil(PILImage.open(BytesIO(bytes_)))

from_path classmethod

from_path(path: Union[str, bytes, PathLike[str], PathLike[bytes]]) -> Self

Create an image from a file path.

This method calls pytauri.image.Image.from_pil internally.

Source code in python/pytauri/src/pytauri/image.py
@classmethod
def from_path(
    cls, path: Union[str, bytes, PathLike[str], PathLike[bytes]], /
) -> Self:
    """Create an image from a file path.

    This method calls [pytauri.image.Image.from_pil][] internally.
    """
    return cls.from_pil(PILImage.open(path))