From b9972136bfa4a72ae561f93782aa8bfc2ca087c8 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 29 Sep 2019 16:46:03 +0200 Subject: [PATCH] Add image metadata detection --- organize_my_photos.py | 27 ++++++++++++++++++++------- setup.py | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/organize_my_photos.py b/organize_my_photos.py index e407709..6d77249 100644 --- a/organize_my_photos.py +++ b/organize_my_photos.py @@ -4,9 +4,22 @@ # Libraries import os import click +import datetime import time import calendar import shutil +from PIL import Image + +def get_date_created(filename): + image = Image.open(filename) + image.verify() + # Format date d/m/Y + if image._getexif()[36867]: + # Metadata + return time.strftime('%d/%m/%Y', datetime.datetime.strptime(image._getexif()[36867], "%Y:%m:%d %H:%M:%S").timetuple()) + else: + # Create data file + return time.strftime('%d/%m/%Y', time.gmtime(os.path.getctime(filename))) EXTENSIONS = ('jpg', 'jpeg', 'gif') @@ -30,14 +43,14 @@ def organize_my_photos(path, locale, extension): # Get path file origin = os.path.join(root, file) # Get date - date_mod = time.strftime('%d/%m/%Y', time.gmtime(os.path.getmtime(os.path.join(root, file)))) - date_list = date_mod.split('/') - date_mod_day = date_list[0] - date_mod_month = date_list[1] - date_mod_year = date_list[2] + date_created = get_date_created(os.path.join(root, file)) + date_list = date_created.split('/') + date_created_day = date_list[0] + date_created_month = date_list[1] + date_created_year = date_list[2] # Make folder: format year/month day -> 2018/abr 23/photo.jpg - dest_folder = os.path.join(date_mod_year, f'{calendar.month_name[int(date_mod_month)]} {date_mod_day}') - dest = os.path.join(date_mod_year, f'{calendar.month_name[int(date_mod_month)]} {date_mod_day}', file) + dest_folder = os.path.join(date_created_year, f'{calendar.month_name[int(date_created_month)]} {date_created_day}') + dest = os.path.join(date_created_year, f'{calendar.month_name[int(date_created_month)]} {date_created_day}', file) if not os.path.exists(dest_folder): os.makedirs(dest_folder) # Move photo diff --git a/setup.py b/setup.py index b6bfd74..c603f8e 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name = 'Organize-my-photos', py_modules=['organize_my_photos'], - version = '1.0.1', + version = '1.0.3', python_requires='>3.6', description = 'Terminal program that organizes and organizes the photographs in folders by year, month and day.', author = 'Andros Fenollosa',