#!/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 import os import csv import argparse from PIL import Image import piexif import piexif.helper # SIFACTOR for GQ geiger counters # 300 series: 0.0065 µSv/h / CPM # 320 series: 0.0065 µSv/h / CPM # 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 # Configure argument parser for cli options parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='''A tool that writes radiation levels (and optionally geocoordinates) to image files and extracts the infos from external sources.''') parser.add_argument('-si', '--sifactor', type=float, default=0.0065, help='Factor to multiply recorded CPM with.') 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.') parser.add_argument('-o', '--outdir', type=str, default='.', help='Directory to output processed photos') args = parser.parse_args() # Import GeigerCounter log with open(args.csv, "r") as f: csvreader = csv.reader(filter(lambda row: row[0]!='#', f), delimiter=',', skipinitialspace=True) for photo in args.photos: # Load Image and EXIF data im = Image.open(photo) exif_dict = piexif.load(im.info['exif']) photo_basename = os.path.basename(photo) picrawtime = exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal].decode('ASCII') picisotime = datetime.strptime(picrawtime, "%Y:%m:%d %H:%M:%S") print('Processing file:', photo_basename, end='\r') for _, csvrawtime, csvrawcpm, _ in csvreader: csvisotime = datetime.fromisoformat(csvrawtime) if csvisotime == picisotime: rad = round(float(csvrawcpm) * args.sifactor, 2) # convert str to exif compatible string new_comment = 'Radiation ☢ ' + str(rad) + ' µS/h' 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) # be os aware and use the correct directory delimiter outfile = os.path.join(args.outdir, photo_basename) im.save(outfile, exif=exif_bytes) # print output print('{:<30} {:<20} {:<22}'.format(photo_basename, str(picisotime), new_comment)) break else: print('{:<30} {:<20} {:<22}'.format(photo_basename, str(picisotime), 'NOT FOUND!'))