Sort image list alphabetically
This commit is contained in:
@@ -1,58 +1,60 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.validators import get_available_image_extensions
|
from django.core.validators import get_available_image_extensions
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
|
|
||||||
gallery_path = Path(settings.MEDIA_ROOT) / settings.GALLERY["path"]
|
gallery_path = Path(settings.MEDIA_ROOT) / settings.GALLERY["path"]
|
||||||
gallery_path_url = Path(settings.MEDIA_URL) / settings.GALLERY["path"]
|
gallery_path_url = Path(settings.MEDIA_URL) / settings.GALLERY["path"]
|
||||||
gallery_thumb_path = Path(settings.MEDIA_ROOT) / settings.GALLERY["thumb_path"]
|
gallery_thumb_path = Path(settings.MEDIA_ROOT) / settings.GALLERY["thumb_path"]
|
||||||
gallery_thumb_path_url = Path(settings.MEDIA_URL) / settings.GALLERY["thumb_path"]
|
gallery_thumb_path_url = Path(settings.MEDIA_URL) / settings.GALLERY["thumb_path"]
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
size = (320, 320)
|
size = (320, 320)
|
||||||
|
|
||||||
Image.logger.setLevel(level=logging.INFO)
|
Image.logger.setLevel(level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def get_image_list(folder_name: str) -> list:
|
def get_image_list(folder_name: str) -> list:
|
||||||
image_path = Path(gallery_path) / folder_name
|
image_path = Path(gallery_path) / folder_name
|
||||||
thumb_path = Path(gallery_thumb_path) / folder_name
|
thumb_path = Path(gallery_thumb_path) / folder_name
|
||||||
img_list = []
|
img_list = []
|
||||||
|
|
||||||
if not Path(image_path).exists():
|
if not Path(image_path).exists():
|
||||||
logger.info("Image path '%s' not found.", image_path)
|
logger.info("Image path '%s' not found.", image_path)
|
||||||
return img_list
|
return img_list
|
||||||
|
|
||||||
Path(thumb_path).mkdir(exist_ok=True)
|
Path(thumb_path).mkdir(exist_ok=True)
|
||||||
|
|
||||||
for _file in os.listdir(image_path):
|
for _file in os.listdir(image_path):
|
||||||
if Path(_file).suffix.lower()[1:] not in get_available_image_extensions():
|
if Path(_file).suffix.lower()[1:] not in get_available_image_extensions():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
thumb_file_path = Path(thumb_path) / f"thumb_{_file}"
|
thumb_file_path = Path(thumb_path) / f"thumb_{_file}"
|
||||||
if not Path(thumb_file_path).exists():
|
if not Path(thumb_file_path).exists():
|
||||||
with Image.open(Path(image_path) / _file, "r") as im:
|
with Image.open(Path(image_path) / _file, "r") as im:
|
||||||
if im._getexif() is not None:
|
if im._getexif() is not None:
|
||||||
im = ImageOps.exif_transpose(im)
|
im = ImageOps.exif_transpose(im)
|
||||||
|
|
||||||
thumb = ImageOps.fit(im, size, Image.Resampling.LANCZOS)
|
thumb = ImageOps.fit(im, size, Image.Resampling.LANCZOS)
|
||||||
thumb.save(thumb_file_path)
|
thumb.save(thumb_file_path)
|
||||||
logger.info("Save thumb 'thumb_%s'.", _file)
|
logger.info("Save thumb 'thumb_%s'.", _file)
|
||||||
|
|
||||||
img_dict = {
|
img_dict = {
|
||||||
"title": _file,
|
"title": _file,
|
||||||
"image_url": Path(gallery_path_url) / folder_name / _file,
|
"image_url": Path(gallery_path_url) / folder_name / _file,
|
||||||
"thumb_url": Path(gallery_thumb_path_url) / folder_name / f"thumb_{_file}",
|
"thumb_url": Path(gallery_thumb_path_url) / folder_name / f"thumb_{_file}",
|
||||||
}
|
}
|
||||||
img_list.append(img_dict)
|
img_list.append(img_dict)
|
||||||
|
|
||||||
return img_list
|
# Sort images alphabetically by filename (case-insensitive) to ensure consistent ordering.
|
||||||
|
# Directory listings may return files in different orders.
|
||||||
|
return sorted(img_list, key=lambda x: x["title"].lower())
|
||||||
def get_folder_list():
|
|
||||||
if Path(gallery_path).exists():
|
|
||||||
return next(os.walk(gallery_path))[1]
|
def get_folder_list():
|
||||||
|
if Path(gallery_path).exists():
|
||||||
return None
|
return next(os.walk(gallery_path))[1]
|
||||||
|
|
||||||
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user