You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.5 KiB

11 months ago
from exif import Image
import pyexiv2
import math
import fractions
import datetime
import gpxpy
import gpxpy.gpx
import pytz
import os
class Fraction(fractions.Fraction):
"""Only create Fractions from floats.
>>> Fraction(0.3)
Fraction(3, 10)
>>> Fraction(1.1)
Fraction(11, 10)
"""
def __new__(cls, value, ignore=None):
"""Should be compatible with Python 2.6, though untested."""
return fractions.Fraction.from_float(value).limit_denominator(99999)
def dms_to_decimal(degrees, minutes, seconds, sign=' '):
"""Convert degrees, minutes, seconds into decimal degrees.
>>> dms_to_decimal(10, 10, 10)
10.169444444444444
>>> dms_to_decimal(8, 9, 10, 'S')
-8.152777777777779
"""
return (-1 if sign[0] in 'SWsw' else 1) * (
float(degrees) +
float(minutes) / 60 +
float(seconds) / 3600
)
def decimal_to_dms(decimal):
"""Convert decimal degrees into degrees, minutes, seconds.
>>> decimal_to_dms(50.445891)
[Fraction(50, 1), Fraction(26, 1), Fraction(113019, 2500)]
>>> decimal_to_dms(-125.976893)
[Fraction(125, 1), Fraction(58, 1), Fraction(92037, 2500)]
"""
remainder, degrees = math.modf(abs(decimal))
remainder, minutes = math.modf(remainder * 60)
return [Fraction(n) for n in (degrees, minutes, remainder * 60)]
def writeLatLonToImage(filename, lat, lon):
GPS = 'Exif.GPSInfo.GPS'
md = pyexiv2.ImageMetadata(filename)
md.read()
#md[GPS + 'AltitudeRef'] = '0' if self.altitude >= 0 else '1'
#md[GPS + 'Altitude'] = Fraction(self.altitude)
md[GPS + 'Latitude'] = decimal_to_dms(lat)
md[GPS + 'LatitudeRef'] = 'N' if lat >= 0 else 'S'
md[GPS + 'Longitude'] = decimal_to_dms(lon)
md[GPS + 'LongitudeRef'] = 'E' if lon >= 0 else 'W'
#md[GPS + 'MapDatum'] = 'WGS-84'
md.write()
def getLatLonFromGPXFile(filename, datetime):
gpx_file = open(filename, 'r')
gpx = gpxpy.parse(gpx_file)
minDiffSecound = 600
nearestpoint = None
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
elapsedTime = datetime - point.time
seconds = elapsedTime.total_seconds()
#print(str(point.time) + ": " +str(seconds))
if seconds < 0:
seconds = seconds * -1
if seconds < minDiffSecound:
nearestpoint = point
minDiffSecound = seconds
return nearestpoint
errorCout = 0
#53 minuten
def updateImage(filename):
global errorCout
global updated
md = pyexiv2.ImageMetadata(filename)
md.read()
print(md.exif_keys)
sys.exit()
#for i in md:
# print(i)
tz = pytz.timezone('Europe/Berlin')
phototime = tz.localize((md["Exif.Image.DateTime"].value - datetime.timedelta(hours=0, minutes=54)))
print("Phototime: "+str(phototime))
point = getLatLonFromGPXFile("track.gpx", phototime)
if point != None:
writeLatLonToImage(filename, point.latitude, point.longitude)
updated = updated + 1
else:
errorCout = errorCout + 1
print("Cant find location!!!!")
updated = 0
#updateImage("DSC07296.JPG")
for entry in os.scandir('.'):
print("> "+entry.name)
if entry.name.endswith(".JPG"):
updateImage(entry.name)
print(entry.name + " updated")
if entry.name.endswith(".ARW"):
updateImage(entry.name)
print(entry.name + " updated")
print("Errors: "+str(errorCout))
print("Updated: "+str(updated))