Made optional Values optional in Exif class.

This commit is contained in:
Marcus Scholz 2020-03-14 20:21:44 +01:00
parent f6a54a5855
commit 13555a0505
2 changed files with 38 additions and 29 deletions

View File

@ -107,7 +107,7 @@ class Exif:
Exif-Comment that has been written (incl. radiation)
'''
def __init__(self, photo, radiation, latitude, longitude, elevation, dry_run):
def __init__(self, photo, dry_run, radiation=False, latitude=False, longitude=False, elevation=False):
self.write_exif = self._write_exif(photo, radiation, latitude,
longitude, elevation, dry_run)
@ -128,36 +128,45 @@ class Exif:
second = round((t1 - minute) * 60, 5)
return (deg, minute, second, loc_value)
def _write_exif(self, photo, radiation, latitude, longitude, elevation, dry_run):
def _write_exif(self, photo, dry_run, radiation, latitude, longitude, elevation):
metadata = pyexiv2.ImageMetadata(photo)
metadata.read()
latitude_degree = self._to_degree(latitude, ["S", "N"])
longitude_degree = self._to_degree(longitude, ["W", "E"])
if latitude or longitude:
latitude_degree = self._to_degree(latitude, ["S", "N"])
longitude_degree = self._to_degree(longitude, ["W", "E"])
# convert decimal coordinates into fractions required for pyexiv2
exiv2_latitude = (Fraction(latitude_degree[0] * 60 + latitude_degree[1], 60),
Fraction(int(round(latitude_degree[2] * 100, 0)), 6000),
Fraction(0, 1))
exiv2_longitude = (Fraction(longitude_degree[0] * 60 + longitude_degree[1], 60),
Fraction(int(round(longitude_degree[2] * 100, 0)), 6000),
Fraction(0, 1))
# convert decimal coordinates into fractions required for pyexiv2
exiv2_latitude = (Fraction(latitude_degree[0] * 60 + latitude_degree[1], 60),
Fraction(int(round(latitude_degree[2] * 100, 0)), 6000),
Fraction(0, 1))
exiv2_longitude = (Fraction(longitude_degree[0] * 60 + longitude_degree[1], 60),
Fraction(int(round(longitude_degree[2] * 100, 0)), 6000),
Fraction(0, 1))
# Set new UserComment
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
# Exif tags to write
metadata['Exif.GPSInfo.GPSLatitude'] = exiv2_latitude
metadata['Exif.GPSInfo.GPSLatitudeRef'] = latitude_degree[3]
metadata['Exif.GPSInfo.GPSLongitude'] = exiv2_longitude
metadata['Exif.GPSInfo.GPSLongitudeRef'] = longitude_degree[3]
metadata['Exif.GPSInfo.GPSAltitude'] = Fraction(elevation)
metadata['Exif.GPSInfo.GPSAltitudeRef'] = '0'
metadata['Exif.Image.GPSTag'] = 654
metadata['Exif.GPSInfo.GPSMapDatum'] = "WGS-84"
metadata['Exif.GPSInfo.GPSVersionID'] = '2 0 0 0'
metadata['Exif.Photo.UserComment'] = new_comment
# Exif tags to write
metadata['Exif.GPSInfo.GPSLatitude'] = exiv2_latitude
metadata['Exif.GPSInfo.GPSLatitudeRef'] = latitude_degree[3]
metadata['Exif.GPSInfo.GPSLongitude'] = exiv2_longitude
metadata['Exif.GPSInfo.GPSLongitudeRef'] = longitude_degree[3]
metadata['Exif.Image.GPSTag'] = 654
metadata['Exif.GPSInfo.GPSMapDatum'] = "WGS-84"
metadata['Exif.GPSInfo.GPSVersionID'] = '2 0 0 0'
if not elevation:
metadata['Exif.GPSInfo.GPSAltitude'] = Fraction(elevation)
metadata['Exif.GPSInfo.GPSAltitudeRef'] = '0'
else:
latitude_degree = None
longitude_degree = None
if radiation:
# Set new UserComment
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
metadata['Exif.Photo.UserComment'] = new_comment
else:
new_comment = None
# Write Exif tags to file, if not in dry-run mode
if dry_run is not True:

View File

@ -90,13 +90,13 @@ for src_photo in args.photos:
print(photo.get_target_photo, photo.get_date)
# Here the matching magic has to happen
latitude = 51.0234024
longitude = 7.248347
#latitude = 51.0234024
#longitude = 7.248347
radiation = 9001.15
elevation = 56.079345703125
#elevation = 56.079345703125
# Write exif data
exif_tags = Exif(photo.get_target_photo, radiation, latitude, longitude, elevation, args.dry)
exif_tags = Exif(photo=photo.get_target_photo, dry_run=args.dry)
print(exif_tags)
# Print table header