From 6deb7c28b9460ceab98e4eeecaf7631da46c469f Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Mon, 6 Sep 2021 17:04:19 +0300 Subject: [PATCH] Add updating exif info --- image_classifier/image_classifier.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/image_classifier/image_classifier.py b/image_classifier/image_classifier.py index 412c6d0..c802f7b 100755 --- a/image_classifier/image_classifier.py +++ b/image_classifier/image_classifier.py @@ -70,13 +70,15 @@ class image_classifier: self._log.debug(f"Found {len(people)} known people in the image.") self._log.debug(json.dumps(people, indent=2)) with open(file, 'rb') as image_file: - exif_info = exif.Image(image_file) - if exif_info.has_exif: - for key in exif_info.list_all(): + self.exif_info = exif.Image(image_file) + if self.exif_info.has_exif: + for key in self.exif_info.list_all(): if key != 'cannot read a base/unknown IFD tag instance': sys.stdout.write(f"{key}: ") - sys.stdout.write(exif_info[key]) - #exif_info["user_comment"] + sys.stdout.write(self.exif_info[key]) + self.append_people_to_exif(people) + with open(file, 'wb') as new_image_file: + new_image_file.write(self.exif_info.get_file()) else: self._log.debug("No exif info in the image.") # get date @@ -84,6 +86,18 @@ class image_classifier: else: self._log.debug("Doesn't seem to be an image.") + def append_people_to_exif(self, people): + if self.is_json(self.exif_info['user_comment']): + data = json.loads(self.exif_info['user_comment']) + if 'PeopleDetected' not in data: + data['PeopleDetected'] = list() + else: + data = {"previous_user_comment": self.exif_info['user_comment']} + data['PeopleDetected'] = list() + for person in people: + data['PeopleDetected'].append(person) + self.exif_info["user_comment"] = json.dumps(data) + def is_json(self, data): result = json.loads(data) return True