🐛 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 io
import os import os
import uuid import uuid
import hashlib
client = pymongo.MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD) client = pymongo.MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD)
db = client[MONGODB_DATABASE] db = client[MONGODB_DATABASE]
@ -22,11 +23,29 @@ def create_test_image(color='red', size=(100, 100)):
img_byte_arr.seek(0) img_byte_arr.seek(0)
return img_byte_arr return img_byte_arr
def save_image(image_data, filename): def process_image(file_path: str) -> tuple[str, str]:
filepath = os.path.join(IMAGES_DIR, filename) """Traite et optimise l'image"""
with open(filepath, 'wb') as f: with Image.open(file_path) as img:
f.write(image_data.getvalue()) # Convertir en RGB si nécessaire
return filepath 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(): def populate_data():
users_collection = db["users"] users_collection = db["users"]
@ -83,47 +102,75 @@ def populate_data():
image_x = create_test_image(color='green') image_x = create_test_image(color='green')
image_y = create_test_image(color='yellow') image_y = create_test_image(color='yellow')
# Sauvegarder les images dans le système de fichiers # Sauvegarder temporairement les images
image_a_path = save_image(image_a, f"{uuid.uuid4()}.jpg") temp_paths = []
image_b_path = save_image(image_b, f"{uuid.uuid4()}.jpg") for img, name in [(image_a, 'a'), (image_b, 'b'), (image_x, 'x'), (image_y, 'y')]:
image_x_path = save_image(image_x, f"{uuid.uuid4()}.jpg") temp_path = os.path.join(IMAGES_DIR, f"temp_{name}.jpg")
image_y_path = save_image(image_y, f"{uuid.uuid4()}.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 # 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({ image_a_id = images_collection.insert_one({
"path": image_a_path, "pin_id": pin_a.inserted_id,
"content_type": "image/jpeg", "image_hash": image_hashes[0],
"exif_date": datetime.now(), "metadata": {
"caption": "Tour Eiffel", "created_at": datetime.now().isoformat(),
"user_id": str(user1_id), "original_filename": "test_a.jpg",
"pin_id": str(pin_a.inserted_id) "mime_type": "image/jpeg",
"size": len(image_a.getvalue())
},
"caption": "Tour Eiffel"
}).inserted_id }).inserted_id
image_b_id = images_collection.insert_one({ image_b_id = images_collection.insert_one({
"path": image_b_path, "pin_id": pin_b.inserted_id,
"content_type": "image/jpeg", "image_hash": image_hashes[1],
"exif_date": datetime.now(), "metadata": {
"caption": "Mont St Michel", "created_at": datetime.now().isoformat(),
"user_id": str(user1_id), "original_filename": "test_b.jpg",
"pin_id": str(pin_b.inserted_id) "mime_type": "image/jpeg",
"size": len(image_b.getvalue())
},
"caption": "Mont St Michel"
}).inserted_id }).inserted_id
image_x_id = images_collection.insert_one({ image_x_id = images_collection.insert_one({
"path": image_x_path, "pin_id": pin_x.inserted_id,
"content_type": "image/jpeg", "image_hash": image_hashes[2],
"exif_date": datetime.now(), "metadata": {
"caption": "Eiffel Tower", "created_at": datetime.now().isoformat(),
"user_id": str(user2_id), "original_filename": "test_x.jpg",
"pin_id": str(pin_x.inserted_id) "mime_type": "image/jpeg",
"size": len(image_x.getvalue())
},
"caption": "Eiffel Tower"
}).inserted_id }).inserted_id
image_y_id = images_collection.insert_one({ image_y_id = images_collection.insert_one({
"path": image_y_path, "pin_id": pin_y.inserted_id,
"content_type": "image/jpeg", "image_hash": image_hashes[3],
"exif_date": datetime.now(), "metadata": {
"caption": "Mont Saint Michel", "created_at": datetime.now().isoformat(),
"user_id": str(user2_id), "original_filename": "test_y.jpg",
"pin_id": str(pin_y.inserted_id) "mime_type": "image/jpeg",
"size": len(image_y.getvalue())
},
"caption": "Mont Saint Michel"
}).inserted_id }).inserted_id
# Mettre à jour les pins avec les IDs des images # Mettre à jour les pins avec les IDs des images

Loading…
Cancel
Save