2020-03-01 17:17:37 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Iterates over a bunch of .jpg or .cr2 files and matches
|
|
|
|
DateTimeOriginal from Exif tag to DateTime in a csv log
|
|
|
|
of a GeigerMuellerCounter."""
|
|
|
|
|
|
|
|
from datetime import datetime
|
2020-03-01 23:21:52 +01:00
|
|
|
import os
|
2020-03-01 22:03:04 +01:00
|
|
|
import csv
|
2020-03-01 22:35:28 +01:00
|
|
|
import argparse
|
2020-03-01 17:17:37 +01:00
|
|
|
from PIL import Image
|
|
|
|
import piexif
|
|
|
|
import piexif.helper
|
|
|
|
|
2020-03-04 12:51:40 +01:00
|
|
|
# SIFACTOR for GQ Geiger counters
|
2020-03-01 18:05:47 +01:00
|
|
|
|
|
|
|
# 300 series: 0.0065 µSv/h / CPM
|
2020-03-01 17:17:37 +01:00
|
|
|
# 320 series: 0.0065 µSv/h / CPM
|
2020-03-01 18:05:47 +01:00
|
|
|
# 500 series: 0.0065 µSv/h / CPM
|
|
|
|
# 500+ series: 0.0065 µSv/h / CPM for the first tube
|
|
|
|
# 600 series: 0.0065 µSv/h / CPM
|
|
|
|
# 600+ series: 0.002637 µSv/h / CPM
|
|
|
|
|
2020-03-01 23:40:49 +01:00
|
|
|
# Configure argument parser for cli options
|
2020-03-01 23:21:52 +01:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description='''A tool that writes
|
2020-03-01 22:35:28 +01:00
|
|
|
radiation levels (and optionally geocoordinates) to image files
|
|
|
|
and extracts the infos from external sources.''')
|
2020-03-01 23:21:52 +01:00
|
|
|
parser.add_argument('-si', '--sifactor', type=float, default=0.0065,
|
|
|
|
help='Factor to multiply recorded CPM with.')
|
2020-03-01 22:35:28 +01:00
|
|
|
parser.add_argument('csv', metavar='CSV', type=str,
|
|
|
|
help='Geiger counter history file in CSV format.')
|
|
|
|
parser.add_argument('photos', metavar='Photo', type=str, nargs='+',
|
|
|
|
help='One or multiple photo image files to process.')
|
2020-03-01 23:21:52 +01:00
|
|
|
parser.add_argument('-o', '--outdir', type=str, default='.',
|
|
|
|
help='Directory to output processed photos')
|
2020-03-01 22:35:28 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-03-04 08:24:52 +01:00
|
|
|
for photo in args.photos:
|
|
|
|
# Load Image and EXIF data
|
|
|
|
im = Image.open(photo)
|
|
|
|
exif_dict = piexif.load(im.info['exif'])
|
2020-03-04 11:15:58 +01:00
|
|
|
# Get image file name out of path
|
2020-03-04 08:24:52 +01:00
|
|
|
photo_basename = os.path.basename(photo)
|
|
|
|
|
|
|
|
# Import GeigerCounter log
|
|
|
|
with open(args.csv, "r") as f:
|
2020-03-04 11:15:58 +01:00
|
|
|
csvreader = csv.reader(filter(lambda row: row[0] != '#', f),
|
|
|
|
delimiter=',', skipinitialspace=True)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
2020-03-01 23:40:49 +01:00
|
|
|
picrawtime = exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal].decode('ASCII')
|
2020-03-01 22:37:37 +01:00
|
|
|
picisotime = datetime.strptime(picrawtime, "%Y:%m:%d %H:%M:%S")
|
2020-03-02 00:03:12 +01:00
|
|
|
print('Processing file:', photo_basename, end='\r')
|
2020-03-01 22:35:28 +01:00
|
|
|
for _, csvrawtime, csvrawcpm, _ in csvreader:
|
|
|
|
csvisotime = datetime.fromisoformat(csvrawtime)
|
2020-03-04 11:15:58 +01:00
|
|
|
# Process image if its timestamp is found in CSV log
|
2020-03-01 22:35:28 +01:00
|
|
|
if csvisotime == picisotime:
|
2020-03-01 23:21:52 +01:00
|
|
|
rad = round(float(csvrawcpm) * args.sifactor, 2)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
|
|
|
# convert str to exif compatible string
|
2020-03-01 23:40:49 +01:00
|
|
|
new_comment = 'Radiation ☢ ' + str(rad) + ' µS/h'
|
2020-03-01 22:35:28 +01:00
|
|
|
user_comment = piexif.helper.UserComment.dump(new_comment, encoding="unicode")
|
|
|
|
exif_dict["Exif"][piexif.ExifIFD.UserComment] = user_comment
|
|
|
|
|
|
|
|
# compile and write tags
|
|
|
|
exif_bytes = piexif.dump(exif_dict)
|
2020-03-02 00:03:12 +01:00
|
|
|
|
|
|
|
# be os aware and use the correct directory delimiter
|
2020-03-01 23:40:49 +01:00
|
|
|
outfile = os.path.join(args.outdir, photo_basename)
|
2020-03-01 23:21:52 +01:00
|
|
|
im.save(outfile, exif=exif_bytes)
|
2020-03-02 00:03:12 +01:00
|
|
|
# print output
|
|
|
|
print('{:<30} {:<20} {:<22}'.format(photo_basename, str(picisotime), new_comment))
|
2020-03-01 22:35:28 +01:00
|
|
|
break
|
2020-03-01 23:40:49 +01:00
|
|
|
else:
|
2020-03-02 00:03:12 +01:00
|
|
|
print('{:<30} {:<20} {:<22}'.format(photo_basename, str(picisotime), 'NOT FOUND!'))
|
2020-03-04 11:15:58 +01:00
|
|
|
# close CSV file
|
2020-03-04 08:24:52 +01:00
|
|
|
f.close()
|