Last active
February 29, 2024 21:13
-
-
Save jlstanus/3bedcbe0decc0c5adfadb5789fc07354 to your computer and use it in GitHub Desktop.
prepare data for 360 photo sharing platforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import sys | |
import exifread | |
import gpxpy | |
import gpxpy.gpx | |
def has_gps_tags(tags): | |
""" | |
Vérifie si les métadonnées contiennent des informations de localisation GPS. | |
""" | |
if 'GPS GPSLatitude' in tags and 'GPS GPSLongitude' in tags: | |
return True | |
return False | |
def sort_photos_by_gps(source_dir, destination_dir_with_gps, destination_dir_without_gps): | |
""" | |
Trie les photos en fonction de la présence ou non de données de localisation GPS. | |
""" | |
for filename in os.listdir(source_dir): | |
if filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'): | |
filepath = os.path.join(source_dir, filename) | |
with open(filepath, 'rb') as f: | |
tags = exifread.process_file(f) | |
if has_gps_tags(tags): | |
shutil.copy(filepath, os.path.join(destination_dir_with_gps, filename)) | |
else: | |
shutil.copy(filepath, os.path.join(destination_dir_without_gps, filename)) | |
def extract_gps_coordinates(tags): | |
""" | |
Extrait les coordonnées GPS à partir des tags EXIF. | |
""" | |
latitude = tags['GPS GPSLatitude'].values | |
longitude = tags['GPS GPSLongitude'].values | |
lat_deg = latitude[0].num / latitude[0].den | |
lat_min = latitude[1].num / latitude[1].den | |
lat_sec = latitude[2].num / latitude[2].den | |
lon_deg = longitude[0].num / longitude[0].den | |
lon_min = longitude[1].num / longitude[1].den | |
lon_sec = longitude[2].num / longitude[2].den | |
lat = lat_deg + lat_min / 60.0 + lat_sec / 3600.0 | |
lon = lon_deg + lon_min / 60.0 + lon_sec / 3600.0 | |
return lat, lon | |
def create_gpx_track(source_dir, output_file): | |
""" | |
Crée une trace GPX à partir des photos dans le répertoire source. | |
""" | |
gpx = gpxpy.gpx.GPX() | |
for filename in os.listdir(source_dir): | |
if filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'): | |
filepath = os.path.join(source_dir, filename) | |
with open(filepath, 'rb') as f: | |
tags = exifread.process_file(f) | |
if 'GPS GPSLatitude' in tags and 'GPS GPSLongitude' in tags: | |
lat, lon = extract_gps_coordinates(tags) | |
gpx_track_point = gpxpy.gpx.GPXTrackPoint(lat, lon) | |
gpx_track = gpxpy.gpx.GPXTrack() | |
gpx_segment = gpxpy.gpx.GPXTrackSegment() | |
gpx_segment.points.append(gpx_track_point) | |
gpx_track.segments.append(gpx_segment) | |
gpx.tracks.append(gpx_track) | |
with open(output_file, 'w') as f: | |
f.write(gpx.to_xml()) | |
# Chemins des répertoires source et de destination | |
source_directory = sys.argv[1] | |
target_directory = sys.argv[2] | |
destination_directory_with_gps = os.path.join(target_directory, 'gps') | |
destination_directory_without_gps = os.path.join(target_directory, 'nogps') | |
# Créer les répertoires de destination s'ils n'existent pas déjà | |
for directory in [destination_directory_with_gps, destination_directory_without_gps]: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
# Trier les photos | |
sort_photos_by_gps(source_directory, destination_directory_with_gps, destination_directory_without_gps) | |
print("Triage terminé.") | |
# Créer la trace GPX | |
create_gpx_track(destination_directory_with_gps, os.path.join(destination_directory_with_gps, 'cameratrace.gpx')) | |
print("Trace GPX créée avec succès.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment