Added write_exif function, calculates location in degree / minutes.

Assembles metadata info and writes them to the target_image.
This commit is contained in:
2020-03-12 23:25:18 +01:00
parent cafda4cf35
commit 693412316e
2 changed files with 41 additions and 23 deletions

View File

@@ -6,8 +6,8 @@
from datetime import datetime
import os
import shutil
from fractions import Fraction
import pyexiv2
import fractions
class Radiation:
''' Handles CSV processing. '''
@@ -66,7 +66,17 @@ class Photo:
return pic_aware_time
class Exif:
''' Converts, compiles and writes Exif-Tags from given values. '''
''' Converts, compiles and writes Exif-Tags from given values.
Arguments:
photo: file name of photo to modify
radiation: radiation levels in µS/h
latitude: latitude as float
longitude: longitude as float
elevation: elevation as float
dry_run: whether to acutally write (True / False)
'''
def __init__(self, photo, radiation, latitude, longitude, elevation, dry_run):
# self.get_degree = self._to_degree(value, loc)
self.write_exif = self._write_exif(photo, radiation, latitude, longitude, elevation, dry_run)
@@ -93,27 +103,39 @@ class Exif:
metadata = pyexiv2.ImageMetadata(photo)
metadata.read()
lat_deg = self._to_degree(latitude, ["S", "N"])
lng_deg = self._to_degree(longitude, ["W", "E"])
latitude_degree = self._to_degree(latitude, ["S", "N"])
longitude_degree = self._to_degree(longitude, ["W", "E"])
print(lat_deg)
print(lng_deg)
print(latitude_degree)
print(longitude_degree)
# 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
keys = ['Exif.Photo.UserComment', 'Exif.Photo.latitude', 'Exif.Photo.longitude']
# Values to write
values = [new_comment, latitude, longitude]
# Create metadata object with all data to write
#for key, value in zip(keys, values):
# Only create object if there is anything to fill with
# if value is not None:
# metadata[key] = pyexiv2.ExifTag(key, value)
# 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
print(new_comment)
# Write Exif tags to file, if not in dry-run mode
#if dry_run is not True:
# metadata.write()
if dry_run is not True:
metadata.write()
return new_comment