Skip to content

Commit

Permalink
Merge pull request #1 from zaytiri/add-feature-to-have-original-creat…
Browse files Browse the repository at this point in the history
…ed-date-from-images-name

Add feature to have original created date from images name
  • Loading branch information
zaytiri authored Sep 15, 2024
2 parents cba32bf + 07facb8 commit 63085bf
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_<yyyymmdd>_<hhmmss>" to be recognized as the date created of the file.

## [1.0.5] - 2024-01-25

### Fixed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_<yyyymmdd>_<hhmmss>", 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:
Expand Down
11 changes: 10 additions & 1 deletion phulize/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 '\\'
50 changes: 47 additions & 3 deletions phulize/resize.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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):
Expand All @@ -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 '\\'
22 changes: 18 additions & 4 deletions phulize/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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)
18 changes: 18 additions & 0 deletions phulize/utils/operating_system.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion phulize/version/progsettings.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PROG:
VERSION: 1.0.5
VERSION: 1.1.0
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ setuptools>=65.5.1
PyYAML~=6.0
margument~=1.1.4
Pillow~=10.2.0
PySimpleGUI~=4.60.5
PySimpleGUI~=4.60.5
piexif~=1.1.3

0 comments on commit 63085bf

Please sign in to comment.