From ec0ebae6838e5987c80658cfbc7b84100b825b16 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Wed, 8 Sep 2021 15:26:01 +0300 Subject: [PATCH] Add check for image --- image_classifier/image_classifier.py | 71 +++++++++++++++------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/image_classifier/image_classifier.py b/image_classifier/image_classifier.py index c4790b7..5c0cb1f 100755 --- a/image_classifier/image_classifier.py +++ b/image_classifier/image_classifier.py @@ -72,41 +72,44 @@ class image_classifier: if not os.access(file, os.R_OK): self._log.error(f"The file '{file}' is not readable.") else: - self.metadata = pyexiv2.ImageMetadata(file) - self.metadata.read() - dirname = os.path.dirname(os.path.realpath(file)) - filename = os.path.basename(file) - people = self.find_faces(file) - if people: - self._log.debug(f"Found {len(people)} known people in the image.") - self._log.debug(json.dumps(people, indent=2)) - if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys: - self._log.debug(f"People (before): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") - self.append_people(people) - self._log.debug(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") - self.metadata.write() - self._log.debug(f"Updated file '{file}'.") - new_path = False - if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: - original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value - self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})") - new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename) - if not new_path: - match = re.search(r'(?P20[0-9]{2})[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P[0-3]?[0-9])', filename) - if match: - new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) - if not new_path: - match = re.search(r'(?P[0-3]?[0-9])[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P20[0-9]{2})', filename) - if match: - new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) - if not new_path: - new_path = os.path.join(dirname, 'unknown-time', filename) - os.makedirs(os.path.dirname(new_path), exist_ok=True) - if self.no_move == False: - self._log.info(f"Moving file '{file}' to '{new_path}'...") - shutil.move(file, new_path) + if self.is_image(file): + self.metadata = pyexiv2.ImageMetadata(file) + self.metadata.read() + dirname = os.path.dirname(os.path.realpath(file)) + filename = os.path.basename(file) + people = self.find_faces(file) + if people: + self._log.debug(f"Found {len(people)} known people in the image.") + self._log.debug(json.dumps(people, indent=2)) + if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys: + self._log.debug(f"People (before): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") + self.append_people(people) + self._log.debug(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") + self.metadata.write() + self._log.debug(f"Updated file '{file}'.") + new_path = False + if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: + original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value + self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})") + new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename) + if not new_path: + match = re.search(r'(?P20[0-9]{2})[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P[0-3]?[0-9])', filename) + if match: + new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) + if not new_path: + match = re.search(r'(?P[0-3]?[0-9])[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P20[0-9]{2})', filename) + if match: + new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) + if not new_path: + new_path = os.path.join(dirname, 'unknown-time', filename) + os.makedirs(os.path.dirname(new_path), exist_ok=True) + if self.no_move == False: + self._log.info(f"Moving file '{file}' to '{new_path}'...") + shutil.move(file, new_path) + else: + self._log.info(f"NOT moving file '{file}' to '{new_path}' because of --no-move") else: - self._log.info(f"NOT moving file '{file}' to '{new_path}' because of --no-move") + self._log.debug(f"The file '{file}' is not an image.") def print_metadata(self): print("IPTC keys:")