From 6c4626ed500a914ac7ea7f6c4b95fe9ee9bf0873 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Sun, 1 Mar 2020 22:03:04 +0100 Subject: [PATCH] Switched to use csv native lib, simplified code. --- exif_rad.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/exif_rad.py b/exif_rad.py index 330c1b5..900d612 100755 --- a/exif_rad.py +++ b/exif_rad.py @@ -6,6 +6,7 @@ DateTimeOriginal from Exif tag to DateTime in a csv log of a GeigerMuellerCounter.""" from datetime import datetime +import csv from PIL import Image import piexif import piexif.helper @@ -35,24 +36,22 @@ print("Timestamp of image: ", picisotime) # Import GeigerCounter log with open("testdata/test.hisdb.his", "r") as f: - for line in f: - if line[0] != '#': - columns = line.split(',') - csvrawtime = columns[1].lstrip() - csvrawcpm = columns[2].lstrip() - csvisotime = datetime.fromisoformat(csvrawtime) - - if csvisotime == picisotime: - rad = round(float(csvrawcpm) * SIFACTOR, 2) - print("Radiation at time of photo: ", rad, "µS/h") + csvreader = csv.reader(filter(lambda row: row[0]!='#', f), delimiter=',', skipinitialspace=True) + for _, csvrawtime, csvrawcpm, _ in csvreader: + print(csvrawtime, csvrawcpm) + csvisotime = datetime.fromisoformat(csvrawtime) + + 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 + # 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