画像ファイルの情報取得(Ruby を使用)
複数の静止画像ファイルについて,次の情報を取得する Ruby プログラムの例。
- タイムスタンプ (mtime)
- ハッシュ関数
- image compression type
- image class and colorspace
- image depth
- number of unique colors
- current width in pixels
- current height in pixels
- x resolution
- y resolution
#! ruby -Ks
# coding: windows-31j
# usage: ruby imagefileinfo.rb filelist.rb > LIST.txt
# This program
# generate a image file list including its image file attributes
# generate image file list
# input : a YAML file that discribe pattern(s) of file names
# [example of "filelist.rb"]
# $a = <<-EOS
# - path: /home/www/teaimage/20100524syouhin
# pattern: *.JPG
# - path: /home/www/teaimage/20100524syouhin_data
# pattern: *.JPG
# EOS
# output : image file list with image information
require 'rubygems'
require 'progressbar'
require 'yaml'
require 'digest/sha2'
load ARGV[0]
b = YAML.load( $a )
for i in b
# from a pattern to a file list
files = Dir.glob( i["path"] + "/" + i["pattern"] )
progress_bar = ProgressBar.new( i["path"], files.length )
for j in files
if j[0] = "/" then
path = j
else
path = Dir.pwd + "/" + j
end
basename = File.basename( j )
mtime = File.mtime( j ).strftime('%Y-%m-%d %H:%M:%S')
digest = Digest::SHA512.hexdigest( File.open( path, "rb" ).read )
# print out image file information
printf( "%s %s '%s' %s ", path, basename, mtime, digest )
system( "identify", "-format", "%C %r %z %k %w %h '%x' '%y'", path.gsub("(","\(").gsub(")","\)") )
progress_bar.inc
end
progress_bar.finish
end
使用法の例
- まず、ファイルリストを記述したファイルを作る。
ファイル名は何でも良い
- ファイルリストを読み込ませる
これで、画像情報が一括取得される
ruby imagefileinfo.rb filelist.rb > LIST.txt
- できた画像情報リストを確認する