add dict for image and move path strings to settings

This commit is contained in:
2022-01-03 11:50:19 +00:00
parent 294a2ad56b
commit e8b602fcaf
5 changed files with 87 additions and 69 deletions

View File

@@ -2,47 +2,74 @@ import logging
import os
from PIL import Image, ExifTags, ImageOps
from django.conf import settings
logger = logging.getLogger(__name__)
gallery_path = settings.GALLERY["path"]
gallery_thumb_path = settings.GALLERY["thumb_path"]
valid_images = [".jpg", ".png"]
def get_image_list(file_path):
def get_image_list(folder_name):
file_path = os.path.join(settings.MEDIA_ROOT + "/" + gallery_path, folder_name)
img_list = []
if os.path.exists(file_path):
for f in os.listdir(file_path):
ext = os.path.splitext(f)[1]
for img in os.listdir(file_path):
ext = os.path.splitext(img)[1]
if ext.lower() not in valid_images:
continue
img_list.append(f)
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
img_dict = {
"title": img,
"image_url": os.path.join(
settings.MEDIA_URL + gallery_path, folder_name + "/" + img
),
"thumb_url": thumb_url,
}
img_list.append(img_dict)
return img_list
def create_thumbs(folder_path):
_base_dir = "/home/project/fet2020/fet2020"
_path = "/files/uploads/gallery"
_thumb_path = "/files/uploads/gallery_thumb"
def get_folder_list():
return next(os.walk(settings.MEDIA_ROOT + "/" + gallery_path))[1]
file_path = os.path.join(_base_dir + _path, folder_path)
thumb_path = os.path.join(_base_dir + _thumb_path, folder_path)
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):
thumb_file_path = os.path.join(thumb_path, f"thumb_{f}")
if os.path.exists(thumb_file_path):
continue
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(f"Bild {f} wird bearbeitet.")
logger.info(f"Edit picture '{f}'.")
with Image.open(str(image_path), "r") as image:
if image._getexif() is not None:
@@ -58,18 +85,6 @@ def create_thumbs(folder_path):
elif exif[orientation] == 8:
image = image.rotate(90, expand=True)
thumb = ImageOps.fit(image, (256, 256), Image.ANTIALIAS)
thumb = ImageOps.fit(image, (200, 200), Image.ANTIALIAS)
thumb.save(thumb_file_path)
logger.info(f"Thumb {thumb} wird gespeichert.")
def get_thumb(folder_path, img):
_base_dir = "/home/project/fet2020/fet2020"
_thumb_path = "/files/uploads/gallery_thumb"
thumb_path = os.path.join(_base_dir + _thumb_path, folder_path)
thumb_file_path = os.path.join(thumb_path, f"thumb_{img}")
if os.path.exists(thumb_file_path):
return f"thumb_{img}"
return img
logger.info(f"Save thumb 'thumb_{f}'.")