Over the years, you could say that I’ve gone through a couple phones.
I’ve had to copy & restore data from the old phone to the new phone numerous times.
As a result, my photos have become wildly disorganized. Photo dates no longer match the actual date the photo was taken, and it’s become impossible to quickly find & pull up a specific photo that I have in mind.
Instead of relying on an app (couldn’t find anything to be honest), I wrote a Python script to restore everything back to its original state.
It reads a JPG’s EXIF date and applies it to the accessed/modified file date. If a file doesn’t have valid EXIF data, it reads the date from one of three formats in the file name: YYYYMMDD_HHMMSS or YYYY-MM-DD_HHMMSS or YYYY-MM-DD_HH-MM-SS, then applies that to the file’s timestamp instead, even if the filename should include funky “(0)” or “(1)” characters, etc.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#!/usr/bin/env python import os, re, time, argparse, exifread def date_correction(args): '''Method for Android date correction of /sdcard/DCIM/Camera''' for dirpath, dirnames, filenames in os.walk(args.directory): for filename in [f for f in filenames if f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.mp4') or f.endswith('.3gp')]: fn = os.path.join(dirpath, filename) f = open(fn, 'rb') tags = exifread.process_file(f) if 'EXIF DateTimeOriginal' in tags.keys(): ts = str(tags['EXIF DateTimeOriginal']) else: ts = None if ts is not None: print 'Found date in EXIF metadata: ' + fn + ' (' + ts + ')' ft = time.mktime(time.strptime(ts, '%Y:%m:%d %H:%M:%S')) os.utime(fn, (ft, ft)) else: match = re.search(r'((?P<y1>[0-9]{8}_[0-9]{6})|(?P<y2>[0-9]{4}\-[0-9]{2}\-[0-9]{2}_[0-9]{6})|(?P<y3>[0-9]{4}\-[0-9]{2}\-[0-9]{2}_[0-9]{2}\-[0-9]{2}\-[0-9]{2}))', filename) if match: if match.group('y1') is not None: print 'Found date in filename: ' + fn + ' (' + match.group('y1') + ')' t = match.group('y1') ft = time.mktime(time.strptime(t, '%Y%m%d_%H%M%S')) os.utime(fn, (ft, ft)) elif match.group('y2') is not None: print 'Found date in filename: ' + fn + ' (' + match.group('y2') + ')' t = match.group('y2') ft = time.mktime(time.strptime(t, '%Y-%m-%d_%H%M%S')) os.utime(fn, (ft, ft)) elif match.group('y3') is not None: print 'Found date in filename: ' + fn + ' (' + match.group('y3') + ')' t = match.group('y3') ft = time.mktime(time.strptime(t, '%Y-%m-%d_%H-%M-%S')) os.utime(fn, (ft, ft)) else: print 'Failed to get file date for ' + fn def main(): try: print '\033[92m' + '\nAndroid Photo Date Correction\n' + '\033[0m' parser = argparse.ArgumentParser() required = parser.add_argument_group('required arguments') required.add_argument('-d', '--directory', metavar='[directory]', required=True, help='Photo source directory') args = parser.parse_args() date_correction(args) except ImportError, e: print 'There must be something missing\n' print e.args[0] if __name__ == '__main__': main() |
In order for this script be functional:
1. Make the script executable.
1 2 |
root at rpi in ~ # chmod +x photodates.py |
2. Install the exifread python module.
1 2 3 4 5 6 7 |
root at rpi in ~ # pip install exifread Downloading/unpacking exifread Downloading ExifRead-2.1.2-py2-none-any.whl (47kB): 47kB downloaded Installing collected packages: exifread Successfully installed exifread Cleaning up... |
3. Install an SSH server on your phone and mount with SSHFS.
1 2 3 4 |
root at rpi in ~ # mkdir /media/e root at rpi in ~ # sshfs -p 2222 ssh@10.0.5.116:/storage/emulated/0 /media/e |
4. Go into your phone’s DCIM/Camera directory and execute (don’t forget the two backticks surrounding pwd).
1 2 3 4 5 6 7 8 9 10 11 12 |
root at rpi in ~ # cd /media/e/DCIM/Camera root at rpi in /media/e/DCIM/Camera # ~/photodates -d `pwd` Android Photo Date Correction Found date in EXIF metadata: /media/usb001/BAK/n6p_backup/DCIM/Camera/2010-09-24_23-21-09_489.jpg (2010:09:25 05:21:06) Found date in EXIF metadata: /media/usb001/BAK/n6p_backup/DCIM/Camera/2010-09-29_19-50-24_185.jpg (2010:09:30 01:50:21) Found date in EXIF metadata: /media/usb001/BAK/n6p_backup/DCIM/Camera/2010-10-04_19-35-53_24.jpg (2010:10:05 01:35:51) Found date in EXIF metadata: /media/usb001/BAK/n6p_backup/DCIM/Camera/2010-10-04_21-37-40_163.jpg (2010:10:05 03:37:37) Found date in EXIF metadata: /media/usb001/BAK/n6p_backup/DCIM/Camera/2010-10-07_19-37-45_701.jpg (2010:10:08 02:37:42) |
For 5.5 gigs of photos & video, this took about a minute and a half to complete.
I realize this fix should probably exist as an Android app instead of a linux-based script. If this post gets adequate search volume, perhaps I’ll consider building an app-based solution.
There are 8 comments