add find france photos and move them

This commit is contained in:
Tykayn 2024-07-04 10:24:33 +02:00 committed by tykayn
parent 437e8e80d4
commit ed63e229d6

View File

@ -0,0 +1,50 @@
import os
import shutil
import exifread
import piexif
# Définition du rectangle entourant la France métropolitaine
france_bbox = (42.0, -5.0, 51.0, 10.0) # (lat_min, lon_min, lat_max, lon_max)
# Chemin du répertoire source
source_dir ='source'
# Chemin du répertoire destination
destination_dir = 'destination'
# Crée le répertoire destination si il n'existe pas
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
# Fonction pour déplacer un fichier si il est dans le rectangle de la France
def move_file_if_in_france(filepath, filename):
# Ouvre le fichier image et lit les informations EXIF
with open(filepath, 'rb') as f:
tags = exifread.process_file(f)
# Recherche les informations GPS dans les informations EXIF
gps_tags = tags.get('GPS')
if gps_tags:
gps_lat = gps_tags.get('GPSLatitude')
gps_lon = gps_tags.get('GPSLongitude')
if gps_lat and gps_lon:
gps_lat = gps_lat.values[0].num / gps_lat.values[0].den
gps_lon = gps_lon.values[0].num / gps_lon.values[0].den
if (france_bbox[0] <= gps_lat <= france_bbox[2] and
france_bbox[1] <= gps_lon <= france_bbox[3]):
# Déplace le fichier dans le sous-répertoire "photos_in_france"
dest_subdir = os.path.join(destination_dir, 'photos_in_france', os.path.basename(os.path.dirname(filepath)))
if not os.path.exists(dest_subdir):
os.makedirs(dest_subdir)
shutil.move(filepath, os.path.join(dest_subdir, filename))
print(f"Moved {filename} to {dest_subdir}")
return True
return False
# Parcourt tous les fichiers dans le répertoire source et ses sous-répertoires
for root, dirs, files in os.walk(source_dir):
for filename in files:
# Vérifie si le fichier est une image
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tif')):
filepath = os.path.join(root, filename)
move_file_if_in_france(filepath, filename)