Skip to content

Instantly share code, notes, and snippets.

@mrjjwright
Created March 25, 2010 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjjwright/344135 to your computer and use it in GitHub Desktop.
Save mrjjwright/344135 to your computer and use it in GitHub Desktop.
requires Date.js, parses jhead output to array of objects
# This is a simple little wrapper around the jhead exif reader.
# See http://www.sentex.net/~mwandel/jhead/
# Date.js (http://datejs.org). Remove and the below lines if you don't need js dates
require "./date"
sys: require "sys"
path: require "path"
PATH_TO_JHEAD: "./external/jhead"
jhead_parse: (image_file_or_dir, callback) ->
image_file_or_dir: image_file_or_dir.replace(" ", "\\ ")
cmd: "${PATH_TO_JHEAD} ${image_file_or_dir}"
sys.exec cmd, (err, stdout, stderr) ->
callback(err) if err?
callback(stderr) if stderr?
exifs_raw: stdout.split("\n\n")
exifs: []
for exif_raw in exifs_raw
lines: exif_raw.split("\n")
exif: {}
for line in lines
if line.length <=1 then continue
name_val: line.split(": ")
name: name_val[0].trim()
value: name_val[1].trim()
switch name
when "GPS Latitude" then exif.gps_lat: value
when "GPS Longitude" then exif.gps_long: value
when "File name"
exif.file_name: path.basename(value)
exif.path: value
when "File size" then exif.file_size: value
when "File date" then exif.file_date: parse_jhead_date(value)
when "Camera make" then exif.camera_make: value
when "Camera model" then exif.camera_model: value
when "Date/Time" then exif.date_time: parse_jhead_date(value)
when "Resolution" then exif.resolution: value
when "Orientation" then exif.orientation: value
when "Flash used" then exif.flash_used: value
when "Focal length" then exif.focal_length: value
when "Exposure time" then exif.exposure_time: value
when "Aperture" then exif.aperture: value
when "ISO equiv." then exif.iso_equiv: value
when "Whitebalance" then exif.white_balance: value
when "Metering Mode" then exif.metering_mode: value
when "Exposure" then exif.exposure: value
exifs.push(exif)
callback(null, exifs) if callback?
#parse (e.g. 2010:02:07 11:40:33) into a JS Date
parse_jhead_date: (value) ->
date_str: value[0..3] + " " + value[5..6] + " " + value[8..9] + " " + value[12..18]
return new Date.parse(date_str)
# callback with an error or an array of exif objects with the above properties on them
exports.parse: (image_file_or_dir, callback) ->
jhead_parse(image_file_or_dir, callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment