meringue.thumbnail.generators ¤
Thumbnailer ¤
Thumbnailer(
image_path: Path | ThumbnailSource | Any,
job_chain: JobChainType,
storage: Storage | None = None,
)
Thumbnail generator class.
Todo:
- aliases for job changes
Attributes:
-
image_path–Source image: a model
FieldFile(any storage, including S3) or an absolute local path. -
job_chain–Thumbnail job chain.
-
storage–Thumbnail file storage.
Raises:
-
AttributeError–Local path is not absolute.
Source code in meringue/thumbnail/generators.py
def __init__(
self,
image_path: Path | ThumbnailSource | Any,
job_chain: JobChainType,
storage: Storage | None = None,
):
"""
Attributes:
image_path: Source image: a model `FieldFile` (any storage, including S3) or an
absolute local path.
job_chain: Thumbnail job chain.
storage: Thumbnail file storage.
Raises:
AttributeError: Local path is not absolute.
"""
self.source = ThumbnailSource.from_value(image_path)
self.source_image_path = self.source.path
self.job_chain = job_chain
self.storage = storage or default_storage
self._thumbnail = None
get_thumbnail_image ¤
Makes the thumbnail once and reuses it for every output format of this job chain.
Returns:
-
Image–Pillow image.
make_thumbnail ¤
Makes a thumbnail based on a quest chain.
Raises:
-
WrongActionOrPropertyError–Invalid action or property.
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("Thumbnail is made using job chain: %s", ", ".join(self.job_chain))
# the source is read and decoded only once for all sizes and formats
image = self.source.image
# 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) -> ThumbnailImage
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:
-
ThumbnailImage–Thumbnail image.
Source code in meringue/thumbnail/generators.py
def get_thumbnail(self, out_format: str, **kwargs) -> ThumbnailImage:
"""
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)
# lazy: the source is not read at all if the thumbnail is already saved
image = lazy(self.get_thumbnail_image, Image.Image)()
thumbnail_image = DefaultThumbnailImage(
image_path=self.source,
job_chain=self.job_chain,
out_format=out_format,
image=image,
storage=self.storage,
)
thumbnail_image.save(**kwargs)
return thumbnail_image