2010年03月06日

RMagickを使った画像リサイズプログラム

RMagickを使った画像処理プログラムシリーズ第1弾

指定した画像 を、回転もしくはリサイズして、指定したファイル名で保存するプログラムである。また、ブログ等への公開を想定しているので、EXIF情報を全消去する。

RMagickがよくできているので、実際の処理を記述しているのは、極わずか。はじめから2/3ほどは、オプションの解析やエラー処理ばかりである・・・。

 SYNOPSIS
rmk_resize.rb [OPTION] [FILE]

DESCRIPTION
-h 説明を表示。
-o 出力ファイル名を指定する。 (必須)
-x n 幅を(n)ピクセルに指定する。
-y n 高さを(n)ピクセルに指定する。
-f 縦横比を保持しない。
このオプションを指定せずに、-xと-yを共に指定した場合、アスペクト比を
    保持するために、-yは無視される。
-r r|l #<= RかLのどちらか
画像を90度回転させる。rは時計周り、lは反時計周り。
このオプションを指定した場合、-xと-yの意味は回転した後のサイズを意味する。
つまり、画像の回転はresizeより先に適用される。
また、-xや-yが指定されずに-rだけ指定された場合は、回転変換のみ行う。
rmk_resize.rb
#!/usr/bin/ruby
# Image File Resizer
#   using RMagick
#               ver.0.4  2010/03/11 Yasuki
#
require 'RMagick'
require 'optparse'
require 'pp'
include Magick

class CommandLine
  attr_accessor:height, :width
  attr_accessor:inputfile, :outputfile
  attr_accessor:rotate, :check

  def initialize
    @height=0
    @width=0
    @outputfile=""
    @rotate=""
    @check=0
  end
end     # of Class CommandLine

#--------------------------------------------------
# Do
option_struct=CommandLine.new

opt=OptionParser.new
  opt.banner="This program is resizer and rotater of a Image file."
  opt.version="0.4"

  opt.on("-x width(px)","--width", "Width px of a new image.") { |v|
    option_struct.width=v.to_i
  }
  opt.on("-y height(px)", "--height","Height px of a new image.") { |v|
    option_struct.height=v.to_i
  }
  opt.on("-o FILENAME", "--output", "Output file name.") { |v|
    option_struct.outputfile=v
  }
  opt.on("-r WAY", "--rotate", ["r","l"], "Image rotatate right or left.") { |v|
    option_struct.rotate=v
  }
  opt.on("-f", "--free", "The aspect ratio is made free. ") { |v|
    option_struct.check=1
  }
  opt.on_tail("-h", "--help", "Show this message.") { |v|
    puts opt
    exit 1
  }
  opt.on_tail("-v", "--version", "Show version.") { |v|
    puts opt.version
    exit 1
  }

opt.parse!(ARGV)
if ARGV[0]==nil then
  puts "The input file is not specified."
  exit 1
else
  option_struct.inputfile=ARGV[0]
end

if option_struct.outputfile=="" then
  puts "The output file is not specified."
  puts opt
  exit 1
end

if FileTest::exist?(option_struct.outputfile) then
  puts "The output file is exist."
  exit 1
end

puts "Option Parse..."
pp option_struct
puts ""

#------------------------------------------------
#
# 画像ファイルの読み込み
img = Magick::ImageList.new(option_struct.inputfile)
img.strip!                # EXIF情報の消去
x=img.columns
y=img.rows
p "Input img: x=#{x}, y=#{y}"

# 計算開始時刻の取得
st=Time.now

# 回転の実施
case option_struct.rotate
when "r" then
  img=img.rotate!(90)
when "l" then
  img=img.rotate!(-90)
end

# リサイズの実施
if (option_struct.width!=0) || (option_struct.height!=0) then 
  if option_struct.check==1 then
    img.resize!(option_struct.width,option_struct.height)
  else
    img.resize_to_fit!(option_struct.width, option_struct.height)
  end
end

# 画像ファイルの出力
img.write(option_struct.outputfile)

# 出力した画像の確認
img = Magick::ImageList.new(option_struct.outputfile)
x=img.columns
y=img.rows
p "Output img: x=#{x}, y=#{y}"

# 計算時間の表示
p "time=#{Time.now - st}sec"

exit 0




sylphide_ffr31mr at 00:42コメント(0)トラックバック(0)programing | 写真 

トラックバックURL

コメントする

名前
 
  絵文字
 
 
記事検索
最新コメント
プロフィール

やすき

月別アーカイブ
  • ライブドアブログ