From 8bb91c564133d0745dcfdc4e0a143772b200f12f Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Tue, 27 Jun 2023 13:58:28 +0300 Subject: [PATCH] skip if there is no folder date time --- image_classifier/image_classifier.py | 76 ++++++++++++++-------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/image_classifier/image_classifier.py b/image_classifier/image_classifier.py index 1f03b5d..c8e3201 100755 --- a/image_classifier/image_classifier.py +++ b/image_classifier/image_classifier.py @@ -103,7 +103,7 @@ class image_classifier: if more_files: files = files + more_files except PermissionError as error: - self._log.warning(f"Permission denied accessing folder '{path}'") + self._log.warning(f"Permission denied accessing folder '{path}'. {error}") return files def process_metadata(self, file): @@ -119,7 +119,7 @@ class image_classifier: ''' Obtain the file date from EXIF metadata or the file name. Return None if it's not accessible or 'unknown-time' if it can't determine the date''' file_date = None - dirname = os.path.dirname(os.path.realpath(file)) + # dirname = os.path.dirname(os.path.realpath(file)) filename = os.path.basename(file) if not os.access(file, os.R_OK): self._log.error(f"The file '{file}' is not readable.") @@ -130,10 +130,10 @@ class image_classifier: self._log.debug(f"File creation time in EXIF: {original_date} \ (type: {type(original_date)})") try: - #file_date = original_date.strftime('%Y/%Y.%m.%d') + # file_date = original_date.strftime('%Y/%Y.%m.%d') file_date = original_date except Exception as error: - self._log.error(f"Failed to convert EXIF information about date '{original_date}'.") + self._log.error(f"Failed to convert EXIF information about date '{original_date}'. {error}") file_date = None if file_date is None: self._log.debug('Date not stored in EXIF metadata') @@ -157,7 +157,6 @@ class image_classifier: self._log.debug(f"Time based folder name section '{file_date}'") return file_date - def process_file(self, file): ''' Process a file, find faces, add EXIF information and move it to the folder of the day''' @@ -169,43 +168,44 @@ class image_classifier: if self.is_image(file): self.process_metadata(file) folder_date_time = self.get_file_date(file) - folder_date = folder_date_time.strftime(self.folder_date_format) - if folder_date: - if self.is_image(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)) - self.append_people(file, people) + if folder_date_time: + folder_date = folder_date_time.strftime(self.folder_date_format) + if folder_date: + if self.is_image(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)) + self.append_people(file, people) - folder = os.path.join(dirname, folder_date, filename) - new_path = os.path.dirname(folder) - if not os.path.exists(new_path): - os.makedirs(new_path) - if self.people_folder: - for person in people: - person_path = os.path.join(self.people_folder, - person.replace(' ', '.'), - folder_date) - if not os.path.exists(person_path): - os.makedirs(person_path) - self._log.info(f"Copying file '{file}' to person '{person}' folder,\ - '{person_path}'...") + folder = os.path.join(dirname, folder_date, filename) + new_path = os.path.dirname(folder) + if not os.path.exists(new_path): + os.makedirs(new_path) + if self.people_folder: + for person in people: + person_path = os.path.join(self.people_folder, + person.replace(' ', '.'), + folder_date) + if not os.path.exists(person_path): + os.makedirs(person_path) + self._log.info(f"Copying file '{file}' to person '{person}' folder,\ + '{person_path}'...") + try: + shutil.copy(file, person_path) + except FileNotFoundError as error: + self._log.error(f"Error copying. File not found. {error}") + if not self.no_move: + if os.path.exists(os.path.join(new_path, filename)): + self._log.debug(f"Destination '{new_path}/{filename}' exists, removing it...") + os.remove(os.path.join(new_path, filename)) + self._log.info(f"Moving file '{file}' to '{new_path}'...") try: - shutil.copy(file, person_path) + shutil.move(file, new_path) except FileNotFoundError as error: self._log.error(f"Error copying. File not found. {error}") - if not self.no_move: - if os.path.exists(os.path.join(new_path, filename)): - self._log.debug(f"Destination '{new_path}/{filename}' exists, removing it...") - os.remove(os.path.join(new_path, filename)) - self._log.info(f"Moving file '{file}' to '{new_path}'...") - try: - shutil.move(file, new_path) - except FileNotFoundError as error: - self._log.error(f"Error copying. File not found. {error}") - 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") def print_metadata(self): print("IPTC keys:")