Skip to content

Image

ImageAssetParams

Bases: BaseModel

Represents the parameters for an image assets.

position class-attribute instance-attribute

position: Position = Field(default_factory=AbsolutePosition)

The positioning of the text assets in the video.

z_index class-attribute instance-attribute

z_index: int = -1

The z-index of the assets.

crop class-attribute instance-attribute

crop: tuple[int, int, int, int] | None = None

The crop range for the image assets.

as_background class-attribute instance-attribute

as_background: bool = True

Whether the image should be used as a background.

ImageAsset

Bases: BaseAsset[ImageAssetParams]

Represents an image assets with various properties.

type class-attribute instance-attribute

type: Literal['image'] = 'image'

The type of the assets. Defaults to "image".

params class-attribute instance-attribute

params: ImageAssetParams = Field(
    default_factory=ImageAssetParams
)

The parameters for the assets.

width instance-attribute

The width of the image.

height instance-attribute

The height of the image.

from_data classmethod

from_data(
    data: str | bytes,
    *,
    path: PathLike | None = None,
    metadata: dict | None = None,
    mime_type: str | None = None,
    **kwargs: Any
) -> Self

Creates an image asset from data.

Parameters:

Name Type Description Default
data str | bytes

The data of the assets.

required
path PathLike | None

The path to the file.

None
metadata dict | None

The metadata of the assets.

None
mime_type str | None

The MIME type of the assets.

None
kwargs Any

Additional keyword arguments to the constructor.

{}

Returns:

Type Description
Self

The assets.

Source code in src/mosaico/assets/image.py
@classmethod
def from_data(
    cls,
    data: str | bytes,
    *,
    path: PathLike | None = None,
    metadata: dict | None = None,
    mime_type: str | None = None,
    **kwargs: Any,
) -> Self:
    """
    Creates an image asset from data.

    :param data: The data of the assets.
    :param path: The path to the file.
    :param metadata: The metadata of the assets.
    :param mime_type: The MIME type of the assets.
    :param kwargs: Additional keyword arguments to the constructor.
    :return: The assets.
    """
    if not check_user_provided_required_keys(kwargs, ["width", "height"]):
        if isinstance(data, str):
            data = data.encode("utf-8")

        with Image.open(io.BytesIO(data)) as img:
            kwargs["width"], kwargs["height"] = img.size

    return super().from_data(data, path=path, metadata=metadata, mime_type=mime_type, **kwargs)

from_path classmethod

from_path(
    path: PathLike,
    *,
    encoding: str = "utf-8",
    mime_type: str | None = None,
    guess_mime_type: bool = True,
    metadata: dict | None = None,
    **kwargs: Any
) -> Self

Creates an image asset from a file path.

Parameters:

Name Type Description Default
path PathLike

The path to the file.

required
encoding str

The encoding of the file.

'utf-8'
metadata dict | None

The metadata of the assets.

None
mime_type str | None

The MIME type of the assets.

None
guess_mime_type bool

Whether to guess the MIME type.

True
kwargs Any

Additional keyword arguments to the constructor.

{}

Returns:

Type Description
Self

The assets.

Source code in src/mosaico/assets/image.py
@classmethod
def from_path(
    cls,
    path: PathLike,
    *,
    encoding: str = "utf-8",
    mime_type: str | None = None,
    guess_mime_type: bool = True,
    metadata: dict | None = None,
    **kwargs: Any,
) -> Self:
    """
    Creates an image asset from a file path.

    :param path: The path to the file.
    :param encoding: The encoding of the file.
    :param metadata: The metadata of the assets.
    :param mime_type: The MIME type of the assets.
    :param guess_mime_type: Whether to guess the MIME type.
    :param kwargs: Additional keyword arguments to the constructor.
    :return: The assets.
    """
    if not check_user_provided_required_keys(kwargs, ["width", "height"]):
        with Image.open(path) as img:
            kwargs["width"], kwargs["height"] = img.size

    return super().from_path(
        path, encoding=encoding, mime_type=mime_type, guess_mime_type=guess_mime_type, metadata=metadata, **kwargs
    )