Skip to content

Instantly share code, notes, and snippets.

@stringhamc
Created September 3, 2018 01:52
Show Gist options
  • Save stringhamc/944271d8e01fd7aa7677e896687159c4 to your computer and use it in GitHub Desktop.
Save stringhamc/944271d8e01fd7aa7677e896687159c4 to your computer and use it in GitHub Desktop.
Wrote a quick hack to rename images to the datetime string used in Android 8.1 based on EXIF data. This way I can easily merge pictures from my phone and my wife's iPhone.
#!/usr/bin/env python
"""Wrote a quick hack to rename images to the datetime string used in
Android 8.1 based on EXIF data. This way I can easily merge pictures from my
phone and my wife's iPhone.
Copyright 2018 Craig Stringham
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__author__='Craig Stringham'
import piexif
import os
import argparse
from datetime import datetime
import re
datefmt = r'([0-9]{4}):([0-9]{2}):([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})'
datepat = re.compile(datefmt)
def rename_iphone_pic(filename):
"""Simple to to rename the iphone pictures to match Nexus 5x style and to
update the modification date as well"""
info = piexif.load(filename)
datestr = info['0th'][piexif.ImageIFD.DateTime].decode('utf-8')
vals = datepat.match(datestr).groups()
year, month, day, hour, minute, second = [int(v) for v in vals]
newfilename = '{year}-{month:02d}-{day:02d} {hour:02d}.{minute:02d}.{second:02d}.jpg'.format(
year=year, month=month, day=day, hour=hour, minute=minute, second=second)
print('{} -> {}'.format(filename, newfilename))
os.rename(filename, newfilename)
dt = datetime(year, month, day, hour, minute, second)
os.utime(newfilename, (dt.timestamp(), dt.timestamp()))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+')
parser.add_argument('--stoperror', action='store_true')
args = parser.parse_args()
for f in args.files:
try:
rename_iphone_pic(f)
except Exception as e:
print('failed to convert {} because of e'.format(f, type(e).__name__))
if args.stoperror:
raise e
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment