Add check for image

This commit is contained in:
Antonio J. Delgado 2021-09-08 15:26:01 +03:00
parent 388545f613
commit ec0ebae683

View file

@ -72,41 +72,44 @@ class image_classifier:
if not os.access(file, os.R_OK): if not os.access(file, os.R_OK):
self._log.error(f"The file '{file}' is not readable.") self._log.error(f"The file '{file}' is not readable.")
else: else:
self.metadata = pyexiv2.ImageMetadata(file) if self.is_image(file):
self.metadata.read() self.metadata = pyexiv2.ImageMetadata(file)
dirname = os.path.dirname(os.path.realpath(file)) self.metadata.read()
filename = os.path.basename(file) dirname = os.path.dirname(os.path.realpath(file))
people = self.find_faces(file) filename = os.path.basename(file)
if people: people = self.find_faces(file)
self._log.debug(f"Found {len(people)} known people in the image.") if people:
self._log.debug(json.dumps(people, indent=2)) self._log.debug(f"Found {len(people)} known people in the image.")
if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys: self._log.debug(json.dumps(people, indent=2))
self._log.debug(f"People (before): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") if 'Xmp.iptcExt.PersonInImage' in self.metadata.xmp_keys:
self.append_people(people) self._log.debug(f"People (before): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self._log.debug(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})") self.append_people(people)
self.metadata.write() self._log.debug(f"People (after): {self.metadata['Xmp.iptcExt.PersonInImage'].raw_value} (type: {type(self.metadata['Xmp.iptcExt.PersonInImage'].raw_value)})")
self._log.debug(f"Updated file '{file}'.") self.metadata.write()
new_path = False self._log.debug(f"Updated file '{file}'.")
if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: new_path = False
original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys:
self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})") original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value
new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename) self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})")
if not new_path: new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename)
match = re.search(r'(?P<year>20[0-9]{2})[\-/\._]?(?P<month>[0-1]?[0-9])[\-/\._]?(?P<day>[0-3]?[0-9])', filename) if not new_path:
if match: match = re.search(r'(?P<year>20[0-9]{2})[\-/\._]?(?P<month>[0-1]?[0-9])[\-/\._]?(?P<day>[0-3]?[0-9])', filename)
new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) if match:
if not new_path: new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename)
match = re.search(r'(?P<day>[0-3]?[0-9])[\-/\._]?(?P<month>[0-1]?[0-9])[\-/\._]?(?P<year>20[0-9]{2})', filename) if not new_path:
if match: match = re.search(r'(?P<day>[0-3]?[0-9])[\-/\._]?(?P<month>[0-1]?[0-9])[\-/\._]?(?P<year>20[0-9]{2})', filename)
new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) if match:
if not new_path: new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename)
new_path = os.path.join(dirname, 'unknown-time', filename) if not new_path:
os.makedirs(os.path.dirname(new_path), exist_ok=True) new_path = os.path.join(dirname, 'unknown-time', filename)
if self.no_move == False: os.makedirs(os.path.dirname(new_path), exist_ok=True)
self._log.info(f"Moving file '{file}' to '{new_path}'...") if self.no_move == False:
shutil.move(file, new_path) 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: 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): def print_metadata(self):
print("IPTC keys:") print("IPTC keys:")