🐛 Fix images in stub
continuous-integration/drone/push Build is passing Details

nominatim_fix
Alix JEUDI--LEMOINE 3 weeks ago
parent 1b7d9aa94d
commit 01576dd97a

@ -6,6 +6,7 @@ from PIL import Image
import io
import os
import uuid
import hashlib
client = pymongo.MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD)
db = client[MONGODB_DATABASE]
@ -22,11 +23,29 @@ def create_test_image(color='red', size=(100, 100)):
img_byte_arr.seek(0)
return img_byte_arr
def save_image(image_data, filename):
filepath = os.path.join(IMAGES_DIR, filename)
with open(filepath, 'wb') as f:
f.write(image_data.getvalue())
return filepath
def process_image(file_path: str) -> tuple[str, str]:
"""Traite et optimise l'image"""
with Image.open(file_path) as img:
# Convertir en RGB si nécessaire
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
bg = Image.new('RGB', img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[-1])
img = bg
elif img.mode != 'RGB':
img = img.convert('RGB')
# Redimensionner si nécessaire pour le Full HD (1920x1080)
if img.size[0] > 1920 or img.size[1] > 1080:
img.thumbnail((1920, 1080), Image.Resampling.LANCZOS)
# Sauvegarder avec compression
img.save(file_path, 'JPEG', quality=85, optimize=True)
# Calculer le hash du fichier optimisé
with open(file_path, 'rb') as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
return file_hash, 'jpg'
def populate_data():
users_collection = db["users"]
@ -83,47 +102,75 @@ def populate_data():
image_x = create_test_image(color='green')
image_y = create_test_image(color='yellow')
# Sauvegarder les images dans le système de fichiers
image_a_path = save_image(image_a, f"{uuid.uuid4()}.jpg")
image_b_path = save_image(image_b, f"{uuid.uuid4()}.jpg")
image_x_path = save_image(image_x, f"{uuid.uuid4()}.jpg")
image_y_path = save_image(image_y, f"{uuid.uuid4()}.jpg")
# Sauvegarder temporairement les images
temp_paths = []
for img, name in [(image_a, 'a'), (image_b, 'b'), (image_x, 'x'), (image_y, 'y')]:
temp_path = os.path.join(IMAGES_DIR, f"temp_{name}.jpg")
with open(temp_path, 'wb') as f:
f.write(img.getvalue())
temp_paths.append(temp_path)
# Traiter les images et obtenir leurs hashes
image_hashes = []
for temp_path in temp_paths:
file_hash, extension = process_image(temp_path)
final_path = os.path.join(IMAGES_DIR, f"{file_hash}.{extension}")
# Si l'image n'existe pas déjà physiquement, la déplacer
if not os.path.exists(final_path):
os.rename(temp_path, final_path)
else:
os.remove(temp_path)
image_hashes.append(file_hash)
# Insérer les métadonnées des images dans la base de données avec leur pin_id
image_a_id = images_collection.insert_one({
"path": image_a_path,
"content_type": "image/jpeg",
"exif_date": datetime.now(),
"caption": "Tour Eiffel",
"user_id": str(user1_id),
"pin_id": str(pin_a.inserted_id)
"pin_id": pin_a.inserted_id,
"image_hash": image_hashes[0],
"metadata": {
"created_at": datetime.now().isoformat(),
"original_filename": "test_a.jpg",
"mime_type": "image/jpeg",
"size": len(image_a.getvalue())
},
"caption": "Tour Eiffel"
}).inserted_id
image_b_id = images_collection.insert_one({
"path": image_b_path,
"content_type": "image/jpeg",
"exif_date": datetime.now(),
"caption": "Mont St Michel",
"user_id": str(user1_id),
"pin_id": str(pin_b.inserted_id)
"pin_id": pin_b.inserted_id,
"image_hash": image_hashes[1],
"metadata": {
"created_at": datetime.now().isoformat(),
"original_filename": "test_b.jpg",
"mime_type": "image/jpeg",
"size": len(image_b.getvalue())
},
"caption": "Mont St Michel"
}).inserted_id
image_x_id = images_collection.insert_one({
"path": image_x_path,
"content_type": "image/jpeg",
"exif_date": datetime.now(),
"caption": "Eiffel Tower",
"user_id": str(user2_id),
"pin_id": str(pin_x.inserted_id)
"pin_id": pin_x.inserted_id,
"image_hash": image_hashes[2],
"metadata": {
"created_at": datetime.now().isoformat(),
"original_filename": "test_x.jpg",
"mime_type": "image/jpeg",
"size": len(image_x.getvalue())
},
"caption": "Eiffel Tower"
}).inserted_id
image_y_id = images_collection.insert_one({
"path": image_y_path,
"content_type": "image/jpeg",
"exif_date": datetime.now(),
"caption": "Mont Saint Michel",
"user_id": str(user2_id),
"pin_id": str(pin_y.inserted_id)
"pin_id": pin_y.inserted_id,
"image_hash": image_hashes[3],
"metadata": {
"created_at": datetime.now().isoformat(),
"original_filename": "test_y.jpg",
"mime_type": "image/jpeg",
"size": len(image_y.getvalue())
},
"caption": "Mont Saint Michel"
}).inserted_id
# Mettre à jour les pins avec les IDs des images

Loading…
Cancel
Save