diff --git a/functions.py b/functions.py index 539a569..8112c7f 100644 --- a/functions.py +++ b/functions.py @@ -4,6 +4,8 @@ ''' Classes used by main program. ''' from datetime import datetime +import os +import shutil import pyexiv2 class Radiation: @@ -28,12 +30,27 @@ class Radiation: class Photo: ''' Reads and writes Exif metadata''' - def __init__(self, photo, local_timezone): + def __init__(self, photo, local_timezone, dest_dir, dry_run): self.get_date = self._get_creation_date(photo, local_timezone) - self.photo = photo + self.copy_photo = self._copy_photo(photo, dest_dir, dry_run) def __repr__(self): - return 'Photo Creation Date: %s' % str(self.get_date) + return 'Photo: %s Creation Date: %s' % (str(self.copy_photo), str(self.get_date)) + + 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 + elif dest_dir != '.': + shutil.copy(photo, dest_photo) + return dest_photo def _get_creation_date(self, photo, local_timezone): # Load Exif data from photo @@ -46,11 +63,11 @@ class Photo: pic_aware_time = pic_naive_time.astimezone(local_timezone) return pic_aware_time - def write_exif(self, radiation, latitude, longitude, dry_run): + def write_exif(self, photo, radiation, latitude, longitude, dry_run): ''' UNTESTED ! ''' - metadata = pyexiv2.ImageMetadata(self.photo) + metadata = pyexiv2.ImageMetadata(photo) # Set new UserComment new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h' @@ -66,6 +83,6 @@ class Photo: metadata[key] = pyexiv2.ExifTag(key, value) # Write Exif tags to file, if not in dry-run mode - if dry_run == 'True': + if dry_run is not True: metadata.write() return new_comment diff --git a/rad_tag.py b/rad_tag.py index eec245a..8ea2fdd 100755 --- a/rad_tag.py +++ b/rad_tag.py @@ -6,8 +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 ''' -import os -import shutil import csv import argparse import pytz @@ -91,30 +89,13 @@ else: print('Modifying photos in', str(args.outdir), '(copy)') for src_photo in args.photos: - # Get image file name out of path - photo_basename = os.path.basename(src_photo) - - # Decide whether to modify photo in place or to copy it to outdir first - # Then set the destination file as 'photo' to work on - if args.outdir == ".": - photo = src_photo - else: - # Be os aware and use the correct directory delimiter for destfile - dst_photo = os.path.join(args.outdir, photo_basename) - # Don't copy image if in dry-run mode - if args.dry == 'True': - shutil.copy(src_photo, dst_photo) - photo = dst_photo - else: - photo = src_photo - - pic_aware_time = Photo(photo, local_timezone) - print(photo_basename, pic_aware_time) + photo = Photo(src_photo, local_timezone, args.outdir, args.dry) + print(photo) # Here the matching magic has to happen # Write exif data - # exif_tags = Photo.write_exif(radiation, latitude, longitude, args.dry) + # exif_tags = Photo.write_exif(photo, radiation, latitude, longitude, args.dry) # print(exif_tags) # Print table header