public
Created

Rename pictures according to their exif information, taking sequence number into account

  • Download Gist
pict-exif-rename
Python
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 55 56 57 58 59 60 61 62
#!/usr/bin/env python
# encoding: utf-8
 
"""
pict-exif-rename: Rename pictures according to their exif information, taking
sequence number into account.
 
This relies on the presence of the Exif.Panasonic.SequenceNumber tag, so may
not work with all cameras.
 
Author: Aurélien Gâteau <aurelien.gateau@free.fr>
License: GPLv3+
"""
import os
import sys
from optparse import OptionParser
 
import pyexiv2
 
USAGE = "%prog [options] <pict1> [<pict2>...]"
 
def name_from_metadata(image_name):
adir = os.path.dirname(image_name)
metadata = pyexiv2.ImageMetadata(image_name)
metadata.read()
 
date_tag = metadata['Exif.Photo.DateTimeOriginal']
sequence_tag = metadata['Exif.Panasonic.SequenceNumber']
 
new_name = date_tag.value.strftime("%Y-%m-%d_%H-%M-%S")
sequence = sequence_tag.value
if sequence > 0:
new_name += "-n%03d" % sequence
new_name += ".jpg"
 
return os.path.join(adir, new_name)
 
def main():
parser = OptionParser(usage=USAGE)
parser.add_option("--dry-run",
action="store_true", dest="dry_run", default=False,
help="Dry run, print what would be done")
 
(options, args) = parser.parse_args()
if len(args) < 1:
parser.error("Missing args")
 
for name in args:
new_name = name_from_metadata(name)
if new_name == name:
print "%s: no change" % name
else:
print "%s: -> %s" % (name, new_name)
if not options.dry_run:
assert not os.path.exists(new_name)
os.rename(name, new_name)
 
return 0
 
if __name__ == "__main__":
sys.exit(main())
# vi: ts=4 sw=4 et

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.