meringue.thumbnail.images ¤
ThumbnailImage ¤
ThumbnailImage(
image_path: Path,
job_chain: JobChainType,
out_format: str,
image: Image,
storage: Storage | None = None,
)
Bases: AltersData
Thumbnail Image class.
Attributes:
-
image_path
–Source image path.
-
job_chain
–Thumbnail job chain.
-
out_format
–Output image format.
-
image
–Thumbnail PIL image.
-
storage
–File storage.
Source code in meringue/thumbnail/images.py
def __init__(
self,
image_path: Path,
job_chain: JobChainType, # str = "extra@1",
out_format: str,
image: Image,
storage: Storage | None = None,
):
"""
Attributes:
image_path: Source image path.
job_chain: Thumbnail job chain.
out_format: Output image format.
image: Thumbnail PIL image.
storage: File storage.
"""
self.source_image_path = image_path
self.job_chain = job_chain
self.out_format = out_format
self.image = image
self.storage = storage or default_storage
self._saved = None
name
property
¤
name: Path
Relative path to thumbnail.
We split the path into separate directories so as not to dump everything in one directory, which can be a problem with a large count of images.
Returns:
-
Path
–Thumbnail path.
thumbnail_hash
property
¤
thumbnail_hash: str
Calculates the thumbnail hash based on the job chain and source path.
Returns:
-
str
–Thumbnail hash.
mimetype
property
¤
mimetype: str
Return thumbnail mimetype.
Python mimetypes library doesn't know webp =(.
Returns:
-
str
–Thumbnail mimetype.
save ¤
Save image to disk.
Attributes:
-
**kwargs
–Pillow imamge save params.
Source code in meringue/thumbnail/images.py
def save(self, force=False, **kwargs):
"""
Save image to disk.
Attributes:
**kwargs: Pillow imamge save params.
"""
if (not force) and self.saved:
return
image = self.image.copy()
if not self.is_supports_alpha:
image = image.convert("RGB")
params = {
**m_settings.THUMBNAIL_SAVE_PARAMS_BY_FORMAT,
**kwargs,
"format": self.out_format,
}
tmp_image = BytesIO()
image.save(tmp_image, **params)
self.storage.save(self.name, tmp_image)
self._saved = True