Code optimization

This commit is contained in:
2025-02-01 01:21:44 +01:00
parent e68c2eca63
commit ad74ae2c51
14 changed files with 147 additions and 199 deletions

View File

@@ -1,80 +1,56 @@
import logging
import os
from pathlib import Path
from django.conf import settings
from django.core.validators import get_available_image_extensions
from PIL import Image, ImageOps
gallery_path = settings.GALLERY["path"]
gallery_thumb_path = settings.GALLERY["thumb_path"]
gallery_path = Path(settings.MEDIA_ROOT) / 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_url = Path(settings.MEDIA_URL) / settings.GALLERY["thumb_path"]
logger = logging.getLogger(__name__)
size = (320, 320)
valid_images = [".jpg", ".png"]
def get_image_list(folder_name):
file_path = os.path.join(settings.MEDIA_ROOT + gallery_path, folder_name)
def get_image_list(folder_name: str) -> list:
image_path = Path(gallery_path) / folder_name
thumb_path = Path(gallery_thumb_path) / folder_name
img_list = []
if os.path.exists(file_path):
for img in os.listdir(file_path):
ext = os.path.splitext(img)[1]
if ext.lower() not in valid_images:
continue
if not Path(image_path).exists():
logger.info("Image path '%s' not found.", image_path)
return img_list
thumb_path = os.path.join(settings.MEDIA_ROOT + gallery_thumb_path, folder_name)
thumb_file_path = os.path.join(thumb_path, f"thumb_{img}")
if os.path.exists(thumb_file_path):
thumb_url = os.path.join(
settings.MEDIA_URL + gallery_thumb_path,
folder_name + "/" + f"thumb_{img}",
)
else:
thumb_url = None
Path(thumb_path).mkdir(exist_ok=True)
img_dict = {
"title": img,
"image_url": os.path.join(
settings.MEDIA_URL + gallery_path,
folder_name + "/" + img,
),
"thumb_url": thumb_url,
}
for _file in os.listdir(image_path):
if Path(_file).suffix.lower()[1:] not in get_available_image_extensions():
continue
img_list.append(img_dict)
thumb_file_path = Path(thumb_path) / f"thumb_{_file}"
if not Path(thumb_file_path).exists():
with Image.open(Path(image_path) / _file, "r") as im:
if im._getexif() is not None:
im = ImageOps.exif_transpose(im)
thumb = ImageOps.fit(im, size, Image.Resampling.LANCZOS)
thumb.save(thumb_file_path)
logger.info("Save thumb 'thumb_%s'.", _file)
img_dict = {
"title": _file,
"image_url": Path(gallery_path_url) / folder_name / _file,
"thumb_url": Path(gallery_thumb_path_url) / folder_name / f"thumb_{_file}",
}
img_list.append(img_dict)
return img_list
def get_folder_list():
if os.path.exists(settings.MEDIA_ROOT + gallery_path):
return next(os.walk(settings.MEDIA_ROOT + gallery_path))[1]
if Path(gallery_path).exists():
return next(os.walk(gallery_path))[1]
return None
def create_thumbs(folder_path):
file_path = os.path.join(settings.MEDIA_ROOT + gallery_path, folder_path)
thumb_path = os.path.join(settings.MEDIA_ROOT + gallery_thumb_path, folder_path)
if os.path.exists(file_path):
os.makedirs(thumb_path, exist_ok=True)
for f in os.listdir(file_path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
thumb_file_path = os.path.join(thumb_path, f"thumb_{f}")
if os.path.exists(thumb_file_path):
continue
image_path = os.path.join(file_path, f)
logger.info("Edit picture '%s'.", f)
with Image.open(str(image_path), "r") as image:
if image._getexif() is not None:
image = ImageOps.exif_transpose(image)
thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
thumb.save(thumb_file_path)
logger.info("Save thumb 'thumb_%s'.", f)