写真画像のExif情報から撮影日時(DateTimeOriginal)を取得して、画像ファイル名へ反映(リネーム)するRubyスクリプトを書いた。

ソースコード。


#!/usr/bin/env ruby
 
require 'date'
require 'rubygems'
require 'RMagick'
 
def get_exif_date(filepath)
  begin
    photo = Magick::Image.read(filepath).first
    dto = photo.get_exif_by_entry('DateTimeOriginal')
    return DateTime.strptime(dto[0][1], '%Y:%m:%d %H:%M:%S')
  rescue
    return nil
  end
end
 
def get_new_file_path(filepath, datetime)
  datetimestr = datetime.strftime('%Y%m%d%H%M%S')
  #ext = File.extname(filepath)
  #base = File::basename(filepath, extname)
  dir, file = File::split(filepath)
  return "#{dir}/#{datetimestr}_#{file}"
end
 
def rename_by_exif(filepath)
  begin
    datetime = get_exif_date(filepath)
    if datetime != nil
      newfilepath = get_new_file_path(filepath, datetime)
      File.rename(filepath, newfilepath)
      return newfilepath
    else
      return nil
    end
  rescue
    return nil
  end
end
 
ARGV.each{|filepath|
  newfilepath = rename_by_exif(filepath)
  if newfilepath != nil
    # success
    puts "rename: #{filepath} -> #{newfilepath}"
  else
    # failure or untouched
    puts "keep: #{filepath}"
  end
}
 
if ARGV.size < 1
  puts "Usage: ruby #{File.basename(__FILE__)} <files>"
end

元の画像ファイルがこんな感じ。


$ ls -lR
total 8
drwxr-xr-x  5 hoge  staff   170  3 16 17:45 aaa
drwxr-xr-x  5 hoge  staff   170  3 16 17:45 bbb
-rwxr-xr-x  1 hoge  staff  1148  3 16 17:29 rename_by_exif.rb
 
./aaa:
total 10968
-rw-r--r--  1 hoge  staff  1825867  2 28 12:25 IMG_2750.jpg
-rw-r--r--  1 hoge  staff  1898592  2 29 11:30 sample1.jpg
-rw-r--r--  1 hoge  staff  1886322  3 16 17:24 sample2.jpg
 
./bbb:
total 10968
-rw-r--r--  1 hoge  staff  1825867  3 16 17:45 IMG_2750.jpg
-rw-r--r--  1 hoge  staff  1898592  3 16 17:45 sample1.jpg
-rw-r--r--  1 hoge  staff  1886322  3 16 17:45 sample2.jpg

カレントディレクトリ以下にあるファイル一覧を取得して、撮影日時を表す Exif の DateTimeOriginal を含むファイル名へリネーム。
リネームできないファイルはそのまま。


$ ./rename_by_exif.rb 
Usage: ruby rename_by_exif.rb <files>
 
$ find . -name "*" | xargs ./rename_by_exif.rb
keep: .
keep: ./.DS_Store
keep: ./aaa
rename: ./aaa/IMG_2750.jpg -> ./aaa/20120228122553_IMG_2750.jpg
rename: ./aaa/sample1.jpg -> ./aaa/20120229113033_sample1.jpg
keep: ./aaa/sample2.jpg
keep: ./bbb
rename: ./bbb/IMG_2750.jpg -> ./bbb/20120228122553_IMG_2750.jpg
rename: ./bbb/sample1.jpg -> ./bbb/20120229113033_sample1.jpg
keep: ./bbb/sample2.jpg
keep: ./rename_by_exif.rb

名前変換後のファイルがこんな感じ。
元のファイル名の前に日時を表す14桁の数字とアンダースコアを付ける。
ファイル更新日時はそのまま。


$ ls -lR
total 8
drwxr-xr-x  5 hoge  staff   170  3 16 17:46 aaa
drwxr-xr-x  5 hoge  staff   170  3 16 17:46 bbb
-rwxr-xr-x  1 hoge  staff  1148  3 16 17:29 rename_by_exif.rb
 
./aaa:
total 10968
-rw-r--r--  1 hoge  staff  1825867  2 28 12:25 20120228122553_IMG_2750.jpg
-rw-r--r--  1 hoge  staff  1898592  2 29 11:30 20120229113033_sample1.jpg
-rw-r--r--  1 hoge  staff  1886322  3 16 17:24 sample2.jpg
 
./bbb:
total 10968
-rw-r--r--  1 hoge  staff  1825867  3 16 17:45 20120228122553_IMG_2750.jpg
-rw-r--r--  1 hoge  staff  1898592  3 16 17:45 20120229113033_sample1.jpg
-rw-r--r--  1 hoge  staff  1886322  3 16 17:45 sample2.jpg

ちなみに、環境。

・OS: Mac OS X Lion
・Ruby 1.8.7
・Homebrew でインストールした ImageMagick 6.7.5-7
・RubyGems でインストールした RMagick 2.13.1


$ uname -mrsv
Darwin 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64
 
$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
 
$ brew info imagemagick
imagemagick 6.7.5-7
http://www.imagemagick.org
Depends on: pkg-config, jpeg, libtiff, little-cms, jasper
/usr/local/Cellar/imagemagick/6.7.1-1 (1390 files, 32M)
/usr/local/Cellar/imagemagick/6.7.5-7 (1392 files, 32M) *
 
Some tools will complain unless the ghostscript fonts are installed to:
  /usr/local/share/ghostscript/fonts
 
http://github.com/mxcl/homebrew/commits/master/Library/Formula/imagemagick.rb
 
$ sudo gem list rmagick
 
*** LOCAL GEMS ***
 
rmagick (2.13.1)

Ref.

tags: ruby rmagick

Posted by NI-Lab. (@nilab)