exifの解像度を変更してInDesignに貼り込む

 XMLInDesignに流し込む時、画像の大きさが大きすぎるとページ上に表示すらできずにオーバーフローしてしまいます。これだと変更するだけでもたいへんですね。かといって、すべての画像をきちんと計算してPhotosho上で解像度を整えるのもめんどいです。*1
 通常、PhotoshopやWebブラウザでは、画像の内部的な解像度をちゃんと読み取るのが普通です。しかし、InDesignではexif情報の中に解像度があるとそちらを優先的に使って等倍貼り込みしてくれます。

 ということで、ピクセル数の閾値以上はページ内に収まるようにexifの解像度(X ResolutionとY Resolution)を変更するスクリプトです。変更といっても詐称みたいなもので、内部情報は書き換えていません。おうち使いなので危険度高めです。

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

# change_resolution.rb
# 2012-04-02
# 設定したピクセル数を超えたら、解像度を上げる


require 'mini_exiftool'


##設定
defult_res = 96.0 #デフォルト解像度
x_lim = 1000 #横幅のピクセルリミット
y_lim = 400 #高さのピクセルリミット


#設定の型を浮動小数点に
defult_res = defult_res.to_f
x_lim = x_lim.to_f
y_lim = y_lim.to_f

##ループ
my_file_count_total = ARGV.length
my_file_count_succeed = 0 #成功ファイル数
ARGV.each do |my_file|
	tmp_f = MiniExiftool.new(my_file)
	tmp_f_width = tmp_f['ImageWidth'].to_f
	next if tmp_f_width === nil#幅がなければ次へ
	tmp_f_height = tmp_f['ImageHeight'].to_f
	tmp_f_xres = tmp_f['XResolution'].to_f
	tmp_f_xres = defult_res if tmp_f_xres === 0.0 #X Resolutionが空ならデフォルト解像度にする
	
	tmp_xres = tmp_yres = 0.0
	if tmp_f_width > x_lim then
		tmp_xres = ((tmp_f_width / x_lim) * tmp_f_xres * 1000).round / 1000.0
	end
	if tmp_f_height > y_lim then
		tmp_yres = ((tmp_f_height / y_lim) * tmp_f_xres * 1000).round / 1000.0
	end
	
	#解像度のセット
	set_res = [tmp_xres, tmp_yres].max
	if set_res > tmp_f_xres then
		tmp_f['XResolution'] = set_res
		tmp_f['YResolution'] = set_res
		tmp_f.save
		my_file_count_succeed += 1
	end
end

puts "Total Files: #{my_file_count_total}"
puts "Changed Files: #{my_file_count_succeed}"

 これの閾値を決めるためのバブルチャートだったりします。

*1:Photoshop上で計算するっていう定石ももちろんあります^^