From 693412316eb250a9dbd6327c0ea98fb807651d9e Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Thu, 12 Mar 2020 23:25:18 +0100 Subject: [PATCH] Added write_exif function, calculates location in degree / minutes. Assembles metadata info and writes them to the target_image. --- functions.py | 56 ++++++++++++++++++++++++++++++++++++---------------- rad_tag.py | 8 ++------ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/functions.py b/functions.py index fcdb947..3b65f81 100644 --- a/functions.py +++ b/functions.py @@ -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 diff --git a/rad_tag.py b/rad_tag.py index 81b4196..f346055 100755 --- a/rad_tag.py +++ b/rad_tag.py @@ -6,7 +6,6 @@ DateTimeOriginal from Exif tag to DateTime in a csv log of a GeigerMuellerCounter and writes its value to the UserComment Exif tag in µS/h ''' -from datetime import datetime import csv import argparse import pytz @@ -61,7 +60,6 @@ with open(args.csv, "r") as f: for _, csv_raw_time, csv_raw_cpm, _ in csv: radiation = Radiation(csv_raw_time, csv_raw_cpm, local_timezone, args.sifactor) radiation_list.append(radiation) - #print(radiation_list) # close CSV file f.close() @@ -77,7 +75,6 @@ if args.gpx is not None: for point in segment.points: position = [point.time, point.latitude, point.longitude, point.elevation] position_list.append(position) - # print(position_list) # Inform the user about what is going to happen if args.dry is True: @@ -95,12 +92,11 @@ for src_photo in args.photos: # Here the matching magic has to happen latitude = 51.0234024 longitude = 7.248347 - radiation = '9001' - elevation = '55' + radiation = 9001.15 + elevation = 56.079345703125 # Write exif data exif_tags = Exif(photo.get_target_photo, radiation, latitude, longitude, elevation, args.dry) - print(exif_tags) # Print table header print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Exif UserComment')) \ No newline at end of file