40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
|
#!/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)
|