2020-03-09 22:42:08 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
''' Classes used by main program. '''
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
from datetime import datetime
|
2020-03-11 22:26:03 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2020-03-12 23:25:18 +01:00
|
|
|
from fractions import Fraction
|
2020-03-10 21:09:25 +01:00
|
|
|
import pyexiv2
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
class Radiation:
|
2020-03-12 19:24:27 +01:00
|
|
|
''' Handles CSV processing. '''
|
2020-03-09 22:42:08 +01:00
|
|
|
def __init__(self, timestamp, radiation, local_timezone, si_factor):
|
|
|
|
self.timestamp = self._time_conversion(timestamp, local_timezone)
|
2020-03-10 21:09:25 +01:00
|
|
|
self.radiation = self._radiation_conversion(radiation, si_factor)
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s %f µS/h' % (str(self.timestamp), self.radiation)
|
|
|
|
|
|
|
|
def _time_conversion(self, timestamp, local_timezone):
|
|
|
|
csv_naive_time = datetime.fromisoformat(timestamp)
|
|
|
|
# Set timezone
|
|
|
|
csv_aware_time = csv_naive_time.astimezone(local_timezone)
|
|
|
|
return csv_aware_time
|
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
def _radiation_conversion(self, radiation, si_factor):
|
2020-03-09 22:42:08 +01:00
|
|
|
# Convert CP/M to µS/h using si_factor
|
|
|
|
radiation = round(float(radiation) * si_factor, 2)
|
|
|
|
return radiation
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
class Photo:
|
2020-03-12 19:24:27 +01:00
|
|
|
''' Reads and writes Exif metadata. '''
|
2020-03-11 22:26:03 +01:00
|
|
|
def __init__(self, photo, local_timezone, dest_dir, dry_run):
|
2020-03-10 21:09:25 +01:00
|
|
|
self.get_date = self._get_creation_date(photo, local_timezone)
|
2020-03-12 19:24:27 +01:00
|
|
|
self.get_target_photo = self._copy_photo(photo, dest_dir, dry_run)
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2020-03-12 19:24:27 +01:00
|
|
|
return 'Photo: %s Creation Date: %s' % (str(self.get_target_photo), str(self.get_date))
|
2020-03-11 22:26:03 +01:00
|
|
|
|
|
|
|
def _copy_photo(self, photo, dest_dir, dry_run):
|
|
|
|
# Determine where to work on photo and copy it there if needed.
|
|
|
|
# Get image file name out of path
|
|
|
|
photo_basename = os.path.basename(photo)
|
|
|
|
# be os aware and use the correct directory delimiter for destfile
|
|
|
|
dest_photo = os.path.join(dest_dir, photo_basename)
|
|
|
|
|
|
|
|
# Copy photo to dest_dir and return its (new) filename
|
|
|
|
# if not in dry_run mode or if dest_dir is different from src_dir.
|
|
|
|
if dry_run is True:
|
|
|
|
return photo
|
2020-03-12 19:24:27 +01:00
|
|
|
if dest_dir != '.':
|
2020-03-11 22:26:03 +01:00
|
|
|
shutil.copy(photo, dest_photo)
|
|
|
|
return dest_photo
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
def _get_creation_date(self, photo, local_timezone):
|
|
|
|
# Load Exif data from photo
|
|
|
|
metadata = pyexiv2.ImageMetadata(photo)
|
|
|
|
metadata.read()
|
2020-03-12 19:24:27 +01:00
|
|
|
print(metadata)
|
2020-03-10 21:09:25 +01:00
|
|
|
date = metadata['Exif.Photo.DateTimeOriginal']
|
|
|
|
# date.value creates datetime object in pic_naive_time
|
|
|
|
pic_naive_time = date.value
|
|
|
|
# Set timezone
|
|
|
|
pic_aware_time = pic_naive_time.astimezone(local_timezone)
|
|
|
|
return pic_aware_time
|
|
|
|
|
2020-03-12 19:24:27 +01:00
|
|
|
class Exif:
|
2020-03-12 23:25:18 +01:00
|
|
|
''' 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)
|
|
|
|
'''
|
|
|
|
|
2020-03-12 20:35:49 +01:00
|
|
|
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)
|
2020-03-10 21:09:25 +01:00
|
|
|
|
2020-03-12 20:35:49 +01:00
|
|
|
# def __repr__(self):
|
|
|
|
# return 'Photo: %s Creation Date: %s' % (str(self.get_target_photo), str(self.get_date))
|
|
|
|
|
|
|
|
def _to_degree(self, 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
|
|
|
|
minute = int(t1)
|
|
|
|
second = round((t1 - minute) * 60, 5)
|
|
|
|
return (deg, minute, second, loc_value)
|
|
|
|
|
|
|
|
def _write_exif(self, photo, radiation, latitude, longitude, elevation, dry_run):
|
2020-03-10 21:09:25 +01:00
|
|
|
|
2020-03-11 22:26:03 +01:00
|
|
|
metadata = pyexiv2.ImageMetadata(photo)
|
2020-03-12 19:24:27 +01:00
|
|
|
metadata.read()
|
2020-03-12 20:35:49 +01:00
|
|
|
|
2020-03-12 23:25:18 +01:00
|
|
|
latitude_degree = self._to_degree(latitude, ["S", "N"])
|
|
|
|
longitude_degree = self._to_degree(longitude, ["W", "E"])
|
2020-03-12 20:35:49 +01:00
|
|
|
|
2020-03-12 23:25:18 +01:00
|
|
|
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))
|
2020-03-12 20:35:49 +01:00
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
# Set new UserComment
|
|
|
|
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
|
|
|
|
|
2020-03-12 23:25:18 +01:00
|
|
|
# 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)
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
# Write Exif tags to file, if not in dry-run mode
|
2020-03-12 23:25:18 +01:00
|
|
|
if dry_run is not True:
|
|
|
|
metadata.write()
|
2020-03-12 20:35:49 +01:00
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
return new_comment
|