-
Notifications
You must be signed in to change notification settings - Fork 5
/
fix_exif.py
40 lines (29 loc) · 1.22 KB
/
fix_exif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from datetime import datetime
import piexif
import re
import os
import time
folder = './'
def get_datetime(filename):
date_str = filename.split('-')[1]
return datetime.strptime(date_str, '%Y%m%d')
def get_date(filename):
date_str = filename.split('-')[1]
return datetime.strptime(date_str, '%Y%m%d').strftime("%Y:%m:%d %H:%M:%S")
fn_regex = re.compile(r'(IMG|VID)-(\d{8})-WA.*\.(jpe?g|mp4|3gp)')
filenames = [fn for fn in os.listdir(folder) if re.match(fn_regex, fn)]
num_files = len(filenames)
print("Number of files: {}".format(num_files))
for i, filename in enumerate(filenames):
if filename.endswith('mp4') or filename.endswith('3gp'):
date = get_datetime(filename)
modTime = time.mktime(date.timetuple())
os.utime(folder + filename, (modTime, modTime))
elif filename.endswith('jpg') or filename.endswith('jpeg'):
exif_dict = {'Exif': {piexif.ExifIFD.DateTimeOriginal: get_date(filename)}}
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, folder + filename)
num_digits = len(str(num_files))
print("{num:{width}}/{max} - {filename}"
.format(num=i+1, width=num_digits, max=num_files, filename=folder+filename))
print('\nDone!')