From 780a64e035e8d85316fe7c30d97d0f52bb92cbb0 Mon Sep 17 00:00:00 2001 From: zay Date: Sat, 14 Sep 2024 19:52:21 +0200 Subject: [PATCH 1/2] add feature to take into account the original date from an images name. meaning now that if the images name has a format of IMG_, the image will be converted with the correct date created field. --- phulize/output.py | 11 ++++++- phulize/resize.py | 50 +++++++++++++++++++++++++++++-- phulize/search.py | 22 +++++++++++--- phulize/utils/operating_system.py | 18 +++++++++++ requirements.txt | 3 +- 5 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 phulize/utils/operating_system.py diff --git a/phulize/output.py b/phulize/output.py index 7b61cf1..887a0a9 100644 --- a/phulize/output.py +++ b/phulize/output.py @@ -2,6 +2,7 @@ from phulize.utils.bytes_conversion import set_converted_bytes_with_label from phulize.utils.file import File +from phulize.utils.operating_system import OperatingSystem, OperatingSystemEnum class Output: @@ -43,7 +44,7 @@ def process(self, absolute_path_parent): date_now = '[' + str(date.today().year) + '-' + str(date.today().month) + '-' + str(date.today().day) + ' ' + str( datetime.utcnow().hour) + '-' + str( datetime.utcnow().minute) + '-' + str(datetime.utcnow().second) + ']' - self.file = File(absolute_path_parent + '\\output' + date_now + '.txt') + self.file = File(absolute_path_parent + self.get_correct_slash_symbol() + 'output' + date_now + '.txt') self.file.open('a') for photo in self.photos: @@ -76,3 +77,11 @@ def __set_message_with_size(self, message, size): def __add_line(self, message): self.file.write(message + '\n') + + def get_correct_slash_symbol(self): + operating_system = OperatingSystem().get_current() + + if operating_system == OperatingSystemEnum.LINUX: + return '/' + + return '\\' \ No newline at end of file diff --git a/phulize/resize.py b/phulize/resize.py index e5d697b..dbb5900 100644 --- a/phulize/resize.py +++ b/phulize/resize.py @@ -1,5 +1,12 @@ -import PIL from PIL import Image +import PIL +import piexif + +import sys +import os +from datetime import datetime + +from phulize.utils.operating_system import OperatingSystem, OperatingSystemEnum class Resize: @@ -9,25 +16,54 @@ def __init__(self, root, photo, percentage): self.percentage = percentage def do(self): - path = self.root + '\\' + self.photo.name_only + self.photo.extension + path = self.root + self.get_correct_slash_symbol() + self.photo.name_only + self.photo.extension try: if not self.is_image_valid(path): print('The image: ' + path + ', it\'s not a valid image file. Could be corrupted') return False image = Image.open(path) + + file_info = os.stat(path) + + if 'IMG' in self.photo.name_only: + + photo_title_splitted = self.photo.name_only.split('_') + print (photo_title_splitted) + year = photo_title_splitted[1][0:4] + month = photo_title_splitted[1][4:6] + day = photo_title_splitted[1][6:] + hour = photo_title_splitted[2][0:2] + minutes = photo_title_splitted[2][2:4] + seconds = photo_title_splitted[2][4:] + + filename = path + exif_dict = piexif.load(filename) + new_date = datetime(int(year), int(month), int(day), int(hour), int(minutes), int(seconds)).strftime("%Y:%m:%d %H:%M:%S") + piexif.remove(filename) + + exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date + exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date + exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date + exif_bytes = piexif.dump(exif_dict) + quality = (84 * self.percentage / 100) image.save(path, quality=round(quality), optimize=True) self.photo.process_size_after() image.close() + + if 'IMG' in self.photo.name_only: + piexif.insert(exif_bytes, filename) if not self.is_image_valid(path): print('The image: ' + path + ', it\'s not a valid image file. Could be corrupted') return False return True - except (ValueError, OSError, TypeError): + except (ValueError, OSError, TypeError) as e: print('Error resizing ' + path) + print('Forward the following error to this package\'s author:') + print(e) return False def is_image_valid(self, path): @@ -44,3 +80,11 @@ def is_image_valid(self, path): return True except (ValueError, OSError, TypeError): return False + + def get_correct_slash_symbol(self): + operating_system = OperatingSystem().get_current() + + if operating_system == OperatingSystemEnum.LINUX: + return '/' + + return '\\' \ No newline at end of file diff --git a/phulize/search.py b/phulize/search.py index d92c02a..23f6dc6 100644 --- a/phulize/search.py +++ b/phulize/search.py @@ -3,11 +3,12 @@ from phulize.utils.directory import Directory from phulize.utils.folder_hierarchy import FolderHierarchy from phulize.utils.photo_file import PhotoFile +from phulize.utils.operating_system import OperatingSystem, OperatingSystemEnum class Search: def __init__(self, arguments): - self.path = '\\'.join(arguments.path.value.split('/')) + self.path = self.get_correct_path(arguments.path.value) self.extensions = arguments.extensions.value self.cloned_folder = arguments.folder.value self.percentage = arguments.quality.value @@ -22,11 +23,13 @@ def search(self): found_files = False self.folder_hierarchy.create_parent(self.cloned_folder) - for root, dirs, files in main_directory.search_through(): + print(files) + print(dirs) + print(root) for photo in files: current_photo = PhotoFile(photo, root) - + if not self.is_valid(current_photo): continue @@ -41,7 +44,8 @@ def search(self): print('\nAn output file with a summary was created in the following directory: \n\t\t' + self.output.file.path + '') else: print('\nNo files were found with defined extensions.') - + return {} + return {'output_file': self.output.file.path, 'folder': self.folder_hierarchy.clone.root, 'path': self.path} def is_valid(self, photo): @@ -73,3 +77,13 @@ def resize(self, photo, root): else: self.output.add_file(photo) print('Encoding successfully done!') + + def get_correct_path(self, path): + operating_system = OperatingSystem().get_current() + + print(operating_system) + if operating_system == OperatingSystemEnum.LINUX: + return path + + splitted_path_by_slash = path.split('/') + return '\\'.join(splitted_path_by_slash) diff --git a/phulize/utils/operating_system.py b/phulize/utils/operating_system.py new file mode 100644 index 0000000..93656b1 --- /dev/null +++ b/phulize/utils/operating_system.py @@ -0,0 +1,18 @@ +from enum import Enum +import sys + + +class OperatingSystemEnum(Enum): + LINUX = 1 + WINDOWS = 2 + +class OperatingSystem: + + def __init__(self): + if sys.platform.startswith('linux'): + self.operating_system = OperatingSystemEnum.LINUX + elif sys.platform.startswith('win'): + self.operating_system = OperatingSystemEnum.WINDOWS + + def get_current(self): + return self.operating_system diff --git a/requirements.txt b/requirements.txt index 08798a9..c095cb9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ setuptools>=65.5.1 PyYAML~=6.0 margument~=1.1.4 Pillow~=10.2.0 -PySimpleGUI~=4.60.5 \ No newline at end of file +PySimpleGUI~=4.60.5 +piexif~=1.1.3 \ No newline at end of file From 07facb893985c3c88b9ae856e4741cda3eb4b86a Mon Sep 17 00:00:00 2001 From: zay Date: Sun, 15 Sep 2024 11:44:59 +0200 Subject: [PATCH 2/2] modify changelog and readme files and imncreasing version number --- CHANGELOG.md | 5 +++++ README.md | 1 + phulize/version/progsettings.yaml | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d075716..c9191d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed - Removed +## [1.1.0] - 2024-09-15 + +### Added +- feature to use the image's name as the original date created of the file. The image's name must have the format of "IMG__" to be recognized as the date created of the file. + ## [1.0.5] - 2024-01-25 ### Fixed diff --git a/README.md b/README.md index 37fef02..76b7e3b 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ pip3 --no-cache-dir install phulize --- ### Important - The quality of the image to be resized, by default, is 50%, meaning all images to resize will have, approximately, 50% less quality than the original image. This can be change, adding the '--quality' argument with a number between 0 and 100. +- If the file image has the name in the following format: "IMG__", the date is going to be added to the converted image's date-created metadata. This means the metadata of the image is going to be correct in the date_created field. Before this improvement, the date_created would be the date the file was converted since it creates a new file. --- Any additional help can be provided if the following command is run: diff --git a/phulize/version/progsettings.yaml b/phulize/version/progsettings.yaml index 0ba877e..2c21f54 100644 --- a/phulize/version/progsettings.yaml +++ b/phulize/version/progsettings.yaml @@ -1,2 +1,2 @@ PROG: - VERSION: 1.0.5 + VERSION: 1.1.0