RMagickを使う
大きな画像から自動で小さないくつかの画像を作りたくて、画像の変換ライブラリなんかを調べてみる。とりあえず会社にある実験用のDebian機にRMagickを入れてテスト。
・・・調べてみると resize というメソッドがあるので、これでよさそうですね。・・・
Use the change_geometry method to resize an image with constraints such as "maintain the current proportions."
ん? change_geometryを使うのか。
試しにいろんなサイズを作ってみよう。
#! /usr/bin/ruby require 'RMagick' include Magick class ChImg def initialize(n) @imgname = n || "" @imgo = Magick::ImageList.new("#{@imgname}") @imgot = @imgname.split('.')[1] end def gimage(aa) itmp = @imgo.copy if aa.kind_of?(Numeric) then itmp.change_geometry("#{aa}x") do | cols, rows, imgt | imgt.resize!(cols, rows) imgt.write("#{@imgname}-#{aa}.#{@imgot}") end else x,y,c = aa[0],aa[1],aa[2] if c then itmp.change_geometry("#{x}x#{y}") do | cols, rows, imgt | imgt.resize!(cols, rows) imgt.write("#{@imgname}-#{x}-#{y}.#{@imgot}") end else if x / y > itmp.columns / itmp.rows then r = itmp.columns.to_f / x.to_f else r = itmp.rows.to_f / y.to_f end itmp.resize!(itmp.columns / r, itmp.rows / r) itmp.crop!(CenterGravity,x,y) itmp.write("#{@imgname}-#{x}-#{y}.#{@imgot}") end end end end Dir.glob('*\.jpg').each{|f| i = ChImg.new(f) [400,300,[100,133],150].each{|a| i.gimage(a) } }
・・・とりあえず動いた。希望が持てる。