Skip to content

Instantly share code, notes, and snippets.

@joshareed
Created April 14, 2011 13:45
Show Gist options
  • Save joshareed/919513 to your computer and use it in GitHub Desktop.
Save joshareed/919513 to your computer and use it in GitHub Desktop.
def dir = new File(args.length > 0 ? args[0] : "/Users/josh/Desktop/image")
def fps = args.length > 1 ? args[1] as int : 10
def parser = new java.text.SimpleDateFormat(args.length > 2 ? args[2] : "yyyy_MM_dd_-_hh_mm_ss.SSS")
def formatter = new java.text.SimpleDateFormat("hh:mm:ss")
// find all image files and sort them by the date encoded in the filename
def files = [:] as TreeMap
dir.eachFileMatch(~/.*\.jpg/) { file ->
def date = parser.parse(file.name - "_UTF.jpg")
files[date.time] = file
}
// process all images to sequentially named files like frame00000001.jpg, frame00000002.jpg, etc
// fill in any time gaps by duplicating images to ensure frame rate is preserved
def first = files.find { true }
long ms = 1000 / fps
long time = ((long) first.key / 1000l) * 1000l
long id = 0
def last = first.value
def map = identify(last)
files.each { k, v ->
// draw extra frames
while (time > 0 && time < k) {
convert(last, id++, map, formatter.format(new Date(time)))
time += ms
}
// draw this frame
convert(v, id++, map, formatter.format(new Date(time)))
// save our last time and last file
time += ms
last = v
map = identify(v)
}
// call ImageMagick identify to extract the exif:ImageDescription field and return the contents as a map
def identify(file) {
def map = [:]
def process = """identify -format "%[EXIF:ImageDescription]" ${file.absolutePath}""".execute()
process.waitFor()
process.text.split(/\. /).each { term ->
def split = term.replace('"', '').split(/:/)
map[split[0].toLowerCase()] = split[1..-1].join(':')
}
map
}
// call ImageMagick convert to resize and annotate the image
def convert(file, id, map, time) {
def width = 1296
def frame = String.format("frame%08d.jpg", id)
["convert", file.absolutePath,
"-resize", "${width}x",
"-gravity", "southwest",
"-splice", "0x40",
"-pointsize", "24",
"-annotate", "+6+6", "Time: ${time}",
"-gravity", "south",
"-annotate", "+0+6", "File: ${file.name}",
"-gravity", "southeast",
"-annotate", "+6+6", "Depth: ${map.depth}m",
frame
].execute().waitFor()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment