Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #4 #5

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions restore-exif.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import logging
from math import modf
import os
import re
from datetime import datetime
Expand All @@ -14,10 +15,14 @@ def get_datetime(filename):
date_str = filename.split('-')[1]
return datetime.strptime(date_str, '%Y%m%d')

def exif_datestr_to_datetime(exifdate):
return datetime.strptime(exifdate, "%Y:%m:%d %H:%M:%S")

def get_exif_datestr(filename):
return get_datetime(filename).strftime("%Y:%m:%d %H:%M:%S")

def get_part_exif_datestr(filename):
return get_datetime(filename).strftime("%Y:%m:%d")

def get_filepaths(path, recursive):
all_filepaths = []
Expand Down Expand Up @@ -49,8 +54,16 @@ def is_whatsapp_img(filename):
def is_whatsapp_vid(filename):
return bool(vid_filename_regex.match(filename))

def modFile(mod, filepath, filename, exifdate = None):
if mod:
if exifdate:
date = exif_datestr_to_datetime(exifdate)
else:
date = get_datetime(filename)
modTime = date.timestamp()
os.utime(filepath, (modTime, modTime))

def main(path, recursive, mod):
def main(path, recursive, mod, fix):
logger.info('Validating arguments')
if not os.path.exists(path):
raise FileNotFoundError('Path specified does not exist')
Expand Down Expand Up @@ -91,24 +104,30 @@ def main(path, recursive, mod):

try:
exif_dict = piexif.load(filepath)
if exif_dict['Exif'].get(piexif.ExifIFD.DateTimeOriginal):
logger.info('Exif date already exists, skipping')
continue
exif_date = exif_dict['Exif'].get(piexif.ExifIFD.DateTimeOriginal)
if exif_date:
if get_part_exif_datestr(filename) == exif_date.split(b" ")[0]:
logger.info('Exif date already exists, has the wrong date')
if not fix:
modFile(mod, filepath, filename)
continue
else:
logger.info('Exif date already exists, skipping')
modFile(mod, filepath, filename)
continue

exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = get_exif_datestr(
filename)
exif_bytes = piexif.dump(exif_dict)
except piexif.InvalidImageDataError:
logger.warning(f'Invalid image data, skipping')
modFile(mod, filepath, filename)
continue
except ValueError:
logger.warning(f'Invalid exif, overwriting with new exif')
make_new_exif(filename)
piexif.insert(exif_bytes, filepath)
if mod:
date = get_datetime(filename)
modTime = date.timestamp()
os.utime(filepath, (modTime, modTime))
modFile(mod, filepath, filename)

logger.info('Finished processing files')

Expand All @@ -122,10 +141,12 @@ def main(path, recursive, mod):
action='store_true', help='Recursively process media')
parser.add_argument('-m', '--mod', default=False,
action='store_true', help='Set file created/modified date on top of exif for images')
parser.add_argument('-f', '--fix', default=False,
action='store_true', help='If the exif date is different from the filename it will be replaced')
args = parser.parse_args()

logging.basicConfig(
level=logging.INFO, format='%(asctime)s %(name)s %(levelname)s: %(message)s')
logger = logging.getLogger('restore-exif')

main(args.path, recursive=args.recursive, mod=args.mod)
main(args.path, recursive=args.recursive, mod=args.mod, fix=args.fix)