Skip to content

meringue.thumbnail.generators ¤

Thumbnailer ¤

Thumbnailer(
    image_path: Path,
    job_chain: JobChainType,
    storage: Storage | None = None,
)

Thumbnail generator class.

Todo:

  • aliases for job changes

Attributes:

  • image_path

    Source image path.

  • job_chain

    Thumbnail job chain.

  • storage

    File storage.

Source code in meringue/thumbnail/generators.py
def __init__(
    self,
    image_path: Path,
    job_chain: JobChainType,
    storage: Storage | None = None,
):
    """
    Attributes:
        image_path: Source image path.
        job_chain: Thumbnail job chain.
        storage: File storage.
    """

    if not image_path.is_absolute():
        msg = "`image_path` must be absolute"
        raise AttributeError(msg, name="image_path")

    self.source_image_path = image_path
    self.job_chain = job_chain
    self.storage = storage or default_storage

make_thumbnail ¤

make_thumbnail() -> Image

Makes a thumbnail based on a quest chain.

Raises:

Returns:

  • Image

    Pillow image.

Source code in meringue/thumbnail/generators.py
def make_thumbnail(self) -> Image:
    """
    Makes a thumbnail based on a quest chain.

    Raises:
        WrongActionOrPropertyError: Invalid action or property.

    Returns:
        Pillow image.
    """

    logger.debug(f"Thumbnail is made using job chain: {', '.join(self.job_chain)}")

    # openingn and prepearing file
    image = Image.open(self.source_image_path)
    image = image.convert("RGBA")

    # prepearing options
    options = copy.deepcopy(DEFAULT_OPTIONS)
    options[constants.PROP_CURRENT_SIZE] = [float(i) for i in image.size]
    options[constants.PROP_NEW_SIZE] = options[constants.PROP_CURRENT_SIZE].copy()

    # applying modifiers
    for job in self.job_chain:
        if ":" in job:
            # property
            prop, args = job.split(":")
            options.update(m_settings.THUMBNAIL_PROPERTIES[prop](args, options))

        elif job in m_settings.THUMBNAIL_ACTIONS:
            # action
            image = m_settings.THUMBNAIL_ACTIONS[job](image, options)

        else:
            raise WrongActionOrPropertyError(job)

    return image

get_thumbnail ¤

get_thumbnail(
    out_format: str, **kwargs
) -> DefaultThumbnailImage

Generates and returns a preview image in the specified format.

Attributes:

  • out_format

    Output image format.

  • **kwargs

    Output image save extra params.

Raises:

Returns:

  • DefaultThumbnailImage

    Thumbnail image.

Source code in meringue/thumbnail/generators.py
def get_thumbnail(self, out_format: str, **kwargs) -> DefaultThumbnailImage:
    """
    Generates and returns a preview image in the specified format.

    Attributes:
        out_format: Output image format.
        **kwargs: Output image save extra params.

    Raises:
        WrongFormatError: Format is not supported.

    Returns:
        Thumbnail image.
    """

    if out_format not in constants.EXTENSIONS_BY_FORMATS:
        raise WrongFormatError(out_format)

    image = lazy(self.make_thumbnail, Image.Image)()
    thumbnail_image = DefaultThumbnailImage(
        image_path=self.source_image_path,
        job_chain=self.job_chain,
        out_format=out_format,
        image=image,
        storage=self.storage,
    )
    thumbnail_image.save(**kwargs)

    return thumbnail_image