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 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-01 18:05:47 +01:00
|
|
|
# Constants for GQ geiger counters
|
|
|
|
|
|
|
|
# 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 17:17:37 +01:00
|
|
|
SIFACTOR = 0.0065
|
|
|
|
|
2020-03-01 22:35:28 +01:00
|
|
|
parser = argparse.ArgumentParser(description='''A tool that writes
|
|
|
|
radiation levels (and optionally geocoordinates) to image files
|
|
|
|
and extracts the infos from external sources.''')
|
|
|
|
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.')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-03-01 22:37:37 +01:00
|
|
|
# Import GeigerCounter log
|
|
|
|
with open(args.csv, "r") as f:
|
|
|
|
csvreader = csv.reader(filter(lambda row: row[0]!='#', f), delimiter=',', skipinitialspace=True)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
2020-03-01 22:37:37 +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-01 22:35:28 +01:00
|
|
|
|
2020-03-01 22:37:37 +01:00
|
|
|
picrawtime = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode('ASCII')
|
|
|
|
picisotime = datetime.strptime(picrawtime, "%Y:%m:%d %H:%M:%S")
|
2020-03-01 22:35:28 +01:00
|
|
|
|
2020-03-01 22:37:37 +01:00
|
|
|
print("Timestamp of image: ", picisotime)
|
2020-03-01 22:35:28 +01:00
|
|
|
for _, csvrawtime, csvrawcpm, _ in csvreader:
|
|
|
|
csvisotime = datetime.fromisoformat(csvrawtime)
|
2020-03-01 19:16:44 +01:00
|
|
|
|
2020-03-01 22:35:28 +01:00
|
|
|
if csvisotime == picisotime:
|
|
|
|
rad = round(float(csvrawcpm) * SIFACTOR, 2)
|
|
|
|
print("Radiation at time of photo: ", rad, "µS/h")
|
|
|
|
|
|
|
|
# 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
|
|
|
|
print("Going to write comment: " + new_comment)
|
|
|
|
exif_bytes = piexif.dump(exif_dict)
|
|
|
|
im.save("testdata/test.jpg", exif=exif_bytes)
|
|
|
|
break
|