Moved copying of photos to functions.py, simplified decision whether to copy photo or not.

This commit is contained in:
Marcus Scholz 2020-03-11 22:26:03 +01:00
parent 92b08170f2
commit 2ad8b4a1fb
2 changed files with 26 additions and 28 deletions

View File

@ -4,6 +4,8 @@
''' Classes used by main program. ''' ''' Classes used by main program. '''
from datetime import datetime from datetime import datetime
import os
import shutil
import pyexiv2 import pyexiv2
class Radiation: class Radiation:
@ -28,12 +30,27 @@ class Radiation:
class Photo: class Photo:
''' Reads and writes Exif metadata''' ''' 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.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): 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): def _get_creation_date(self, photo, local_timezone):
# Load Exif data from photo # Load Exif data from photo
@ -46,11 +63,11 @@ class Photo:
pic_aware_time = pic_naive_time.astimezone(local_timezone) pic_aware_time = pic_naive_time.astimezone(local_timezone)
return pic_aware_time return pic_aware_time
def write_exif(self, radiation, latitude, longitude, dry_run): def write_exif(self, photo, radiation, latitude, longitude, dry_run):
''' UNTESTED ! ''' ''' UNTESTED ! '''
metadata = pyexiv2.ImageMetadata(self.photo) metadata = pyexiv2.ImageMetadata(photo)
# Set new UserComment # Set new UserComment
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h' new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
@ -66,6 +83,6 @@ class Photo:
metadata[key] = pyexiv2.ExifTag(key, value) metadata[key] = pyexiv2.ExifTag(key, value)
# Write Exif tags to file, if not in dry-run mode # Write Exif tags to file, if not in dry-run mode
if dry_run == 'True': if dry_run is not True:
metadata.write() metadata.write()
return new_comment return new_comment

View File

@ -6,8 +6,6 @@ DateTimeOriginal from Exif tag to DateTime in a csv log
of a GeigerMuellerCounter and writes its value to the UserComment of a GeigerMuellerCounter and writes its value to the UserComment
Exif tag in µS/h ''' Exif tag in µS/h '''
import os
import shutil
import csv import csv
import argparse import argparse
import pytz import pytz
@ -91,30 +89,13 @@ else:
print('Modifying photos in', str(args.outdir), '(copy)') print('Modifying photos in', str(args.outdir), '(copy)')
for src_photo in args.photos: for src_photo in args.photos:
# Get image file name out of path photo = Photo(src_photo, local_timezone, args.outdir, args.dry)
photo_basename = os.path.basename(src_photo) print(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)
# Here the matching magic has to happen # Here the matching magic has to happen
# Write exif data # 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(exif_tags)
# Print table header # Print table header