improved Modal with zoom and pagination. also implementing thumbnail generation
This commit is contained in:
@@ -6,12 +6,17 @@ from PIL import Image
|
||||
import exifread
|
||||
import mimetypes
|
||||
import imagehash
|
||||
import uuid
|
||||
|
||||
from app import db
|
||||
from app.models import ImageRecord
|
||||
|
||||
THUMB_DIR = "/images/thumbs"
|
||||
THUMB_SIZE = (400, 400)
|
||||
|
||||
def import_images_task(source_dir, dest_dir):
|
||||
imported = []
|
||||
os.makedirs(THUMB_DIR, exist_ok=True)
|
||||
|
||||
for artist_dir in os.listdir(source_dir):
|
||||
artist_path = os.path.join(source_dir, artist_dir)
|
||||
@@ -43,9 +48,19 @@ def import_images_task(source_dir, dest_dir):
|
||||
dest_path = os.path.join(target_dir, filename)
|
||||
shutil.copy2(full_path, dest_path)
|
||||
|
||||
# Generate thumbnail
|
||||
thumb_filename = f"{uuid.uuid4().hex}.jpg"
|
||||
thumb_path = os.path.join(THUMB_DIR, thumb_filename)
|
||||
try:
|
||||
generate_thumbnail(full_path, thumb_path)
|
||||
except Exception as e:
|
||||
print(f"[WARN] Failed to generate thumbnail: {e}")
|
||||
thumb_path = None
|
||||
|
||||
record = ImageRecord(
|
||||
filename=filename,
|
||||
filepath=dest_path,
|
||||
thumb_path=thumb_path,
|
||||
hash=file_hash,
|
||||
perceptual_hash=str(phash) if phash else None,
|
||||
artist=artist_dir,
|
||||
@@ -61,11 +76,25 @@ def import_images_task(source_dir, dest_dir):
|
||||
imported.append(filename)
|
||||
|
||||
# Add delay between each image
|
||||
db.session.commit()
|
||||
time.sleep(1)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return f"Imported {len(imported)} images."
|
||||
|
||||
def generate_thumbnail(image_path, size=(400, 400), overwrite=False):
|
||||
thumbnail_dir = THUMB_DIR # update this accordingly
|
||||
basename = os.path.basename(image_path)
|
||||
thumb_path = os.path.join(thumbnail_dir, basename)
|
||||
|
||||
if not overwrite and os.path.exists(thumb_path):
|
||||
return thumb_path
|
||||
|
||||
with Image.open(image_path) as img:
|
||||
img.thumbnail(size)
|
||||
img.save(thumb_path)
|
||||
|
||||
return thumb_path
|
||||
|
||||
def calculate_hash(file_path):
|
||||
hash_func = hashlib.sha256()
|
||||
@@ -74,7 +103,6 @@ def calculate_hash(file_path):
|
||||
hash_func.update(chunk)
|
||||
return hash_func.hexdigest()
|
||||
|
||||
|
||||
def calculate_perceptual_hash(file_path):
|
||||
try:
|
||||
with Image.open(file_path) as img:
|
||||
@@ -83,7 +111,6 @@ def calculate_perceptual_hash(file_path):
|
||||
print(f"[WARN] Failed to compute perceptual hash for {file_path}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def is_similar_image(phash, width, height, threshold=2):
|
||||
for img in ImageRecord.query.filter(ImageRecord.perceptual_hash != None).all():
|
||||
try:
|
||||
@@ -96,7 +123,6 @@ def is_similar_image(phash, width, height, threshold=2):
|
||||
print(f"[WARN] Failed pHash comparison: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def extract_metadata(file_path):
|
||||
metadata = {
|
||||
'file_size': None,
|
||||
@@ -129,4 +155,4 @@ def extract_metadata(file_path):
|
||||
except Exception as e:
|
||||
print(f"[WARN] Failed to read EXIF data: {file_path} – {e}")
|
||||
|
||||
return metadata
|
||||
return metadata
|
||||
Reference in New Issue
Block a user