Skip to content

Instantly share code, notes, and snippets.

@zb3
Created February 4, 2017 16:58
Show Gist options
  • Save zb3/e1b54141d5641f7fb1ec8d9108698fdd to your computer and use it in GitHub Desktop.
Save zb3/e1b54141d5641f7fb1ec8d9108698fdd to your computer and use it in GitHub Desktop.
Recursively strip all EXIF info + metadata, BUT preserve date and time
import os
import sys
import subprocess
# recursively strip all EXIF info + metadata, BUT preserve date and time
# requires jhead (tested on jhead v3.00)
if len(sys.argv) < 2:
print('%s [path]')
exit()
path = sys.argv[1]
def process_file(path):
# save date/time
out = subprocess.check_output(('jhead', path), stderr=subprocess.STDOUT)
dst = None
if b'Date/Time' in out:
lines = out.split(b'\n')
for line in lines:
if line.startswith(b'Date/Time'):
dst = line[line.index(b':')+2:]
break
if not dst:
print('WARNING: No date info in %s' % path)
# do the actual strip
ret = subprocess.run(('jhead', '-purejpg', path))
if ret.returncode:
print('ERROR: Return code was %d for %s' % (ret.returncode, path))
# restore date/time
if dst:
dt = dst.split(b' ')[0].decode('iso-8859-1')
tt = dst.split(b' ')[1].decode('iso-8859-1')
ret = subprocess.run(('jhead', '-mkexif', '-ts'+dst.decode('iso-8859-1').replace(' ', '-'), path))
if ret.returncode:
print('ERROR: While restoring date/time, return code was %d for %s' % (ret.returncode, path))
for root, dirs, files in os.walk(path):
for name in files:
f = os.path.join(root, name)
process_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment