Wednesday, July 4, 2012

Getting the time date stamp into the file name

We just returned from a two week trip to China on which we took 3000 photos with four different cameras. Now we need select a hundred or so to put on our web page. The problem is that most image previewers sort by file name. This means the photos are each grouped by camera.


require 'rubygems'
require 'exifr'

mypix = Dir.glob(ARGV[0] +"/*.{jpg,JPG}")
    somepix = mypix.sort_by \
        { |a| EXIFR::JPEG.new(a).exif.to_hash[:date_time_original] }
    somepix.each do |pic_file| 
 regex = /[\/\.]/
 system("mv " + pic_file + " " + 
            pic_file.to_s.split(regex)[0] + "/" +
            pic_file.to_s.split(regex)[0] + "_" + 
            EXIFR::JPEG.new(pic_file).exif.to_hash[:date_time_original].strftime("%Y-%m-%d_%H%M%S") + 
            ".jpg")

end

The run the app

$ruby rename_pix.rb trip12a


And now you can sort your pix.

No comments:

Post a Comment