get photos from server, optimize creating thumbs, add buttons
This commit is contained in:
74
fet2020/gallery/utils.py
Normal file
74
fet2020/gallery/utils.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import logging
|
||||
import os
|
||||
from PIL import Image, ExifTags, ImageOps
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
valid_images = [".jpg", ".png"]
|
||||
|
||||
|
||||
def get_image_list(file_path):
|
||||
img_list = []
|
||||
|
||||
if os.path.exists(file_path):
|
||||
for f in os.listdir(file_path):
|
||||
ext = os.path.splitext(f)[1]
|
||||
if ext.lower() not in valid_images:
|
||||
continue
|
||||
|
||||
img_list.append(f)
|
||||
|
||||
return img_list
|
||||
|
||||
|
||||
def create_thumbs(folder_path):
|
||||
_base_dir = "/home/project/fet2020/fet2020"
|
||||
_path = "/files/uploads/gallery"
|
||||
_thumb_path = "/files/uploads/gallery_thumb"
|
||||
|
||||
file_path = os.path.join(_base_dir + _path, folder_path)
|
||||
thumb_path = os.path.join(_base_dir + _thumb_path, folder_path)
|
||||
|
||||
if os.path.exists(file_path):
|
||||
os.makedirs(thumb_path, exist_ok=True)
|
||||
|
||||
for f in os.listdir(file_path):
|
||||
logger.info(f"Bild {f} wird bearbeitet.")
|
||||
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
|
||||
|
||||
image_path = os.path.join(file_path, f)
|
||||
|
||||
with Image.open(str(image_path), "r") as image:
|
||||
if image._getexif() is not None:
|
||||
for orientation in ExifTags.TAGS.keys():
|
||||
if ExifTags.TAGS[orientation] == "Orientation":
|
||||
break
|
||||
exif = dict(image._getexif().items())
|
||||
|
||||
if exif[orientation] == 3:
|
||||
image = image.rotate(180, expand=True)
|
||||
elif exif[orientation] == 6:
|
||||
image = image.rotate(270, expand=True)
|
||||
elif exif[orientation] == 8:
|
||||
image = image.rotate(90, expand=True)
|
||||
|
||||
thumb = ImageOps.fit(image, (256, 256), Image.ANTIALIAS)
|
||||
thumb.save(thumb_file_path)
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user