Basic functionality ready.
This commit is contained in:
29
viewer/utils.py
Normal file
29
viewer/utils.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# Standard library imports.
|
||||
from pathlib import Path
|
||||
|
||||
# External library imports.
|
||||
from PIL import Image
|
||||
|
||||
# Django imports.
|
||||
from django.conf import settings
|
||||
|
||||
###########################################################################################
|
||||
# Helper functions. #
|
||||
###########################################################################################
|
||||
|
||||
def make_thumbnail(image):
|
||||
"""
|
||||
Creates a thumbnail in the corresponding THUMBNAILS_ROOT directory for the given image.
|
||||
|
||||
:param image: A pathlib.Path instance pointing to a file inside the GALLERY_ROOT directory.
|
||||
"""
|
||||
|
||||
if image.exists():
|
||||
thumb_path = Path(str(image).replace(str(settings.GALLERY_ROOT), str(settings.THUMBNAILS_ROOT)))
|
||||
|
||||
thumb_path.parent.mkdir(parents = True, exist_ok = True)
|
||||
|
||||
if not thumb_path.exists():
|
||||
with Image.open(str(image)) as pilimg:
|
||||
pilimg.thumbnail(THUMB_SIZE)
|
||||
pilimg.save(str(thumb_path))
|
||||
Reference in New Issue
Block a user