Quilroy

img compare

Sep 2nd, 2024
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import random
  2. import base64
  3. from PIL import Image, ImageChops
  4. import piexif
  5.  
  6. # Liste d'URLs encodées en base64
  7. encoded_urls = [
  8.     "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vdXJsMQ==",
  9.     "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vdXJsMg==",
  10.     "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vdXJsMw==",
  11.     "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vdXJsNA=="
  12. ]
  13.  
  14. # Fonction pour comparer deux images
  15. def compare_images(image1_path, image2_path):
  16.     image1 = Image.open(image1_path).convert('RGB')
  17.     image2 = Image.open(image2_path).convert('RGB')
  18.    
  19.     # Compare les histogrammes
  20.     h1 = image1.histogram()
  21.     h2 = image2.histogram()
  22.    
  23.     rms = sum((a - b) ** 2 for a, b in zip(h1, h2)) ** 0.5
  24.     return rms
  25.  
  26. # Fonction pour insérer un commentaire dans les métadonnées EXIF
  27. def insert_exif_comment(image_path, comment):
  28.     exif_dict = piexif.load(image_path)
  29.    
  30.     # Convertir le commentaire en bytes
  31.     comment_bytes = comment.encode('utf-8')
  32.    
  33.     # Insérer le commentaire dans les métadonnées EXIF
  34.     exif_dict['0th'][piexif.ImageIFD.ImageDescription] = comment_bytes
  35.    
  36.     # Sauvegarder les modifications dans l'image
  37.     exif_bytes = piexif.dump(exif_dict)
  38.     Image.open(image_path).save(image_path, exif=exif_bytes)
  39.  
  40. # Chemins vers les images à comparer
  41. image1_path = "image1.jpg"
  42. image2_path = "image2.jpg"
  43.  
  44. # Comparer les images
  45. rms_value = compare_images(image1_path, image2_path)
  46. threshold = 1000  # Ajustez ce seuil en fonction de la précision souhaitée
  47.  
  48. if rms_value < threshold:
  49.     # Sélectionner une URL aléatoire encodée en base64
  50.     random_comment = base64.b64decode(random.choice(encoded_urls)).decode('utf-8')
  51.    
  52.     # Insérer le commentaire dans les EXIF de la première image
  53.     insert_exif_comment(image1_path, random_comment)
  54.    
  55.     print(f"Les images sont similaires. Commentaire inséré : {random_comment}")
  56. else:
  57.     print("Les images ne sont pas assez similaires pour insérer un commentaire.")
  58.  
Add Comment
Please, Sign In to add comment