Add rename photos script

This commit is contained in:
Gregory Trolliet 2020-08-15 21:48:17 +02:00
parent a000fbac63
commit d18b71082b
1 changed files with 39 additions and 0 deletions

39
.scripts/renamePhotos.py Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Program which should rename photo files according to their creation date.
"""
import os
import exifread
from datetime import datetime
image_ext = ['.jpg', '.jpeg', '.cr2', '.cr3' '.nef', '.dng']
def get_creation_date(image_file_path):
with open(image_file_path, 'rb') as my_picture:
tags = exifread.process_file(my_picture)
try:
return datetime.strptime(str(
tags.get('EXIF DateTimeOriginal')),
'%Y:%m:%d %H:%M:%S')
except ValueError:
print("No value for ", image_file)
return
image_files = [ifile for ifile in os.listdir('.')
if os.path.splitext(ifile)[1].lower() in image_ext]
for image_file in image_files:
file_basename, file_ext = os.path.splitext(image_file)
picture_date = get_creation_date(image_file)
if picture_date:
rename_name = picture_date.strftime('GT_%Y%m%d-%H%M%S_') + file_basename + file_ext
if not os.path.exists(rename_name):
os.rename(image_file, rename_name)