Skip to content

Instantly share code, notes, and snippets.

@gpoulter
Created April 10, 2012 13:22
Show Gist options
  • Save gpoulter/2351325 to your computer and use it in GitHub Desktop.
Save gpoulter/2351325 to your computer and use it in GitHub Desktop.
Set EXIF caption from JPEG filename
#!/usr/bin/env python
"""
1) Start with a directory full of DSC_003242.jpg files from the camera.
2a) Download jhead.exe and save it as C:\jhead.exe
http://www.sentex.net/~mwandel/jhead/
2b) Go to Start Menu, "Run..." and enter "cmd" to open a DOS box. Change
to the image directory, and run "C:\jhead.exe -nf *.jpg" so all the files look like
MMDD-HHMMSS.jpg (0102-131415.jpg was taken at 13:14:15 on 2nd January)
3) Go through the images in your favourite viewer, pressing F2 to rename them,
so they look like this:
MMDD-HHMMSS Short caption;other stuff.jpg
4a) Download and install Python (http://www.python.org)
4b) Place "jpeg-captions.py" in the image directory and double-click it
to create a "descript.ion" file with a line for each .jpg as follows:
"MMDD-HHMMSS Short caption;other stuff.jpg" Short caption
5a) Download the Exifer utility:
http://www.tucows.com/download.html?software_id=281273_106644&download=automatic
5b) Open Exifer, navigate to the image directory, select all .jpg files,
right click, select the option EXIF/IPTC -> Import -> DESCRIPT.ION.
Select "IPTC - Description", then click "Okay". Which imports
the descript.ion file into the IPTC caption field.
Now the short captions in the file name will show up in Picasa, Picasa Web Albums,
Facebook and Gallery. If you edit the captions in Picasa afterwards, I wouldnt
bother updating the file name (which is just a guide for organising the files).
"""
import os
import re
import sys
def files_to_description():
out = file("descript.ion", "wb")
for fname in os.listdir("."):
if os.path.isfile(fname) and fname.endswith(".jpg"):
match = re.match(r"[0-9-]+ (.*?)(;.*)?\.jpg", fname)
if match is not None:
caption = match.group(1)
out.write('"%s" %s\n' % (fname, caption))
out.close()
def description_to_files():
inf = file("descript.ion", "rb")
for line in inf:
line = line.strip()
match = re.match(r'(?:"(.*)"|(.*?)) (.*)', line)
if match is not None:
fn1, fn2, caption = match.groups()
fname = fn1 if fn1 else fn2
print match.groups()
if os.path.isfile(fname):
# Match date, optional (space, caption, optional flags), .jpg
match = re.match(r"([0-9-]+)(?: (.*?)(;.*)?)?\.jpg", fname)
if match is not None:
date, oldcaption, other = match.groups()
newname = date + " " + caption
if other is not None:
newname += other
newname += ".jpg"
if fname != newname:
print fname + " --> " + newname
os.rename(fname, newname)
inf.close()
if __name__ == "__main__":
direction = sys.argv[1]
target = sys.argv[2]
os.chdir(target)
if direction == "f2d":
files_to_description()
elif direction == "d2f":
description_to_files()
else:
print "Direction must be f2d or d2f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment