Rubyで複数の画像のサイズを一括で調べてExcelに書き出す。
さいしょに
Photoshopなどで横長・縦長の画像でアクションを変更したい時に
沢山の画像の中からそれぞれ画像のサイズを調べて、横長・縦長をフォルダに仕分けるのが面倒だったので作りました。
後日キャプチャのせるかも。
機能
調べたい画像のあるディレクトリで実行すると
ディレクトリ内の画像(jpg|jpeg|png)を対象に画像のサイズを調べます。
# オプション
出力はExcelかCSVで書き出せます。
必要であれば、自動で以下のフォルダに画像を仕分けます。
「Same」 縦横のサイズが同じ
「Width」 横長
「Height」 縦長
「Unknown」 なんかよくわからんけど、駄目っぽい
「ERROR」 画像のサイズが読み込めない
。
※ Excelの書き出しはWindowsのみです。
※ ファイルの移動はオリジナルファイルを移動するので注意です。
出力例 Excel
file name | width | height | large |
---|---|---|---|
error.jpg | ? | ? | ERROR |
height19y396f96tf6tef.jpeg | 500 | 501 | Height |
same12312yydsdhf76.png | 500 | 500 | Same |
width126et7tr7w6t67re.jpg | 501 | 500 | Width |
コード Ruby
Github READMEまだ書いてないですが…
# image_size_search.rb
# coding: utf-8 require 'rubygems' require 'image_size' require 'win32ole' class ImageSizeSearch def initialize() @file = [] @file_table = [] @file_table << ['file name', 'width', 'height', 'large'] @path = Dir.pwd.gsub('/', '\\') << '\\' @timestamp = Time.now.to_i end def file_get Dir.glob('*') do |f| # support: bmp, gif, jpeg, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm if /.*?\.(jpg|jpeg|png)/ =~ f @file << f end end end def file_open @file.each do |file| open(file, 'rb') do |f| img = ImageSize.new(f.read) if img.format.nil? @file_table << [file, '?', '?', 'ERROR'] else size = img.width - img.height if size === 0 large = 'Same' elsif size > 0 large = 'Width' elsif size < 0 large = 'Height' else large = 'Unknown' end @file_table << [file, img.width, img.height, large] end end end end def write_csv File.open("image_size_#{@timestamp}.csv", 'w') do |file| @file_table.each do |size| file.puts size.join(',') end end end def write_excel excel = WIN32OLE.new('Excel.Application') # excel.visible = true workbook = excel.workbooks.add @file_table.each_with_index do |array, i| array.each_with_index do |size, j| workbook.sheets[1].rows[i+1].columns[j+1] = size end end workbook.saveAs "#{@path}image_size_search_#{@timestamp}.xlsx" # excel.quit end def mv_file Dir.mkdir("#{@path}Same") unless Dir.exist?("#{@path}Same") Dir.mkdir("#{@path}Width") unless Dir.exist?("#{@path}Width") Dir.mkdir("#{@path}Height") unless Dir.exist?("#{@path}Height") Dir.mkdir("#{@path}Unknown") unless Dir.exist?("#{@path}Unknown") Dir.mkdir("#{@path}ERROR") unless Dir.exist?("#{@path}ERROR") @file_table.each_with_index do |file, i| if !(i === 0) File.rename(file[0], "#{@path}#{file[3]}/#{file[0]}") end end end end iss = ImageSizeSearch.new() iss.file_get iss.file_open # ===== Option ===== # iss.write_csv # iss.write_excel # iss.mv_file # ===== Option =====
準備
- 「image_size」が必要です。
gem install image_size
- 「win32ole」はRuby1.8以上で標準で入ってるみたいです
オプション
- コメントアウトを解除して使用します。 ※複数可
# iss.write_csv ← CSVで書き出す場合 # iss.write_excel ← Excelで書き出す場合 # iss.mv_file ← フォルダに仕分ける場合
使い方
- 「image_size_search.rb」を調べたい画像のあるフォルダに移動させて実行して下さい。
ruby image_size_search.rb
EXE化
- いちいちコマンドで実行するのめんどくせーって言う場合はEXE化してみると良いかも
gem install ocra ocra --windows image_size_search.rb
作成されたEXEファイルを調べたいフォルダに移動させて実行すれば大丈夫です。
さいごに
Ruby全然書いてなかったので、コードが怪しいかも。