Working output mechanism, honors users outdir choice.

If outdir is set, srcfile will be copied to outdir first and modified there.
This commit is contained in:
Marcus Scholz 2020-03-05 15:19:29 +01:00
parent ef6080d64c
commit f46c8682ec
2 changed files with 21 additions and 15 deletions

View File

@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- uses pyexiv2 which is also able to tag various camera raw formats, e. g. CR2
- uses pyexiv2 instead of piexif which is also able to tag various camera raw formats, e. g. CR2
- removed unnecessary datetime object creation as pyexiv2 can output datetime objects
## [0.2] - ??????
@ -23,4 +23,4 @@ First prototype using piexif
- output in a tablish manner
### Modified
- exchanged selfmade CSV parser by python's core CSV library
- exchanged selfmade CSV parser by python's core CSV library

View File

@ -8,6 +8,7 @@ Exif tag in µS/h"""
from datetime import datetime
import os
import shutil
import csv
import argparse
import pyexiv2
@ -37,15 +38,28 @@ parser.add_argument('-o', '--outdir', type=str, default='.',
args = parser.parse_args()
for photo in args.photos:
for srcphoto in args.photos:
# Get image file name out of path
photo_basename = os.path.basename(srcphoto)
# 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 == ".":
print('Going to overwrite source file')
photo = srcphoto
else:
print('Going to copy source file to destdir and modify it there')
# be os aware and use the correct directory delimiter for destfile
dstphoto = os.path.join(args.outdir, photo_basename)
shutil.copy(srcphoto, dstphoto)
photo = dstphoto
# Load Exif data from image
metadata = pyexiv2.ImageMetadata(photo)
metadata.read()
tag = metadata['Exif.Photo.DateTimeOriginal']
# tag.value creates datetime object in pictime
pictime = tag.value
# Get image file name out of path
photo_basename = os.path.basename(photo)
# Import GeigerCounter log
with open(args.csv, "r") as f:
@ -66,16 +80,8 @@ for photo in args.photos:
# print found radiation levels
print('{:<30} {:<20} {:<22}'.format(photo_basename, str(pictime), new_comment))
# be os aware and use the correct directory delimiter for destfile
outfile = os.path.join(args.outdir, photo_basename)
if args.outdir == ".":
print('Going to overwrite source file')
# metadata.write()
else:
print('Going to copy source file to destdir and modify it there')
# Write Exif tags to file
metadata.write()
break
else:
print('{:<30} {:<20} {:<22}'.format(photo_basename, str(pictime), 'NOT FOUND!'))