Created
January 4, 2012 21:48
-
-
Save agateau/1562329 to your computer and use it in GitHub Desktop.
Rename pictures according to their exif information, taking sequence number into account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment