Qual è il modo migliore per geotaggare le immagini jpeg con Python? [chiuso]
-
19-08-2019 - |
Domanda
Ho delle coordinate da qualche fonte e voglio taggare i miei file jpg con loro. Qual è la migliore libreria Python per scrivere geotag in dati exif?
Soluzione
pexif è stato scritto con geotag come obiettivo (la mia enfasi):
pexif è una libreria Python per l'analisi e, soprattutto, la modifica dei dati EXIF ??in file JPEG.
Questo è nato da la necessità di aggiungere dati taggati GPS alle mie immagini , Sfortunatamente le altre librerie là fuori non potevano fare aggiornamenti e non sembravano facilmente progettabili per poter aggiungere un tale cosa. Il software non è riutilizzabile alla grande!
Il mio motivo principale per scrivere questo è stato quello di fornire un modo semplice per geo-taggare le mie foto, e la libreria ora sembra abbastanza matura per farlo.
Altri suggerimenti
Ecco un esempio di come impostare la posizione GPS usando la libreria pyexiv2. Ho testato questo script caricando l'immagine con geotag su Panoramio
#!/usr/bin/env python
import pyexiv2
import fractions
from PIL import Image
from PIL.ExifTags import TAGS
import sys
def to_deg(value, loc):
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
else:
loc_value = ""
abs_value = abs(value)
deg = int(abs_value)
t1 = (abs_value-deg)*60
min = int(t1)
sec = round((t1 - min)* 60, 5)
return (deg, min, sec, loc_value)
def set_gps_location(file_name, lat, lng):
"""Adds GPS position as EXIF metadata
Keyword arguments:
file_name -- image file
lat -- latitude (as float)
lng -- longitude (as float)
"""
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
print lat_deg
print lng_deg
# convert decimal coordinates into degrees, munutes and seconds
exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))
exiv_image = pyexiv2.Image(file_name)
exiv_image.readMetadata()
exif_keys = exiv_image.exifKeys()
exiv_image["Exif.GPSInfo.GPSLatitude"] = exiv_lat
exiv_image["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
exiv_image["Exif.GPSInfo.GPSLongitude"] = exiv_lng
exiv_image["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
exiv_image["Exif.Image.GPSTag"] = 654
exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
exiv_image.writeMetadata()
set_gps_location(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]))
Non l'ho provato da solo, ma dalla documentazione [pyexiv2] [1] sembra che dovrebbe fare il lavoro.
[1]: http://tilloy.net/dev/pyexiv2/tutorial.html #link mancava l'ultimo carattere
il codice sopra funziona, ma ho dovuto modificare la funzione set_gps_location per funzionare con la versione corrente di pyexiv2 ... forse Maksym stava usando una versione precedente:
def set_gps_location(file_name, lat, lng):
"""Adds GPS position as EXIF metadata
Keyword arguments:
file_name -- image file
lat -- latitude (as float)
lng -- longitude (as float)
"""
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
print lat_deg
print lng_deg
# convert decimal coordinates into degrees, munutes and seconds
exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))
metadata = pyexiv2.ImageMetadata(file_name)
metadata.read()
## exif_keys = metadata.exif_keys
metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat
metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lng
metadata["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
metadata["Exif.Image.GPSTag"] = 654
metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
metadata.write()
pyexiv2 è ora deprecato a favore di GExiv2 , basato su GObject wrapper di libexiv2.