About

resize.rb は ImageMagick/GraphicsMagick の機能を Ruby の RMagick 経由で呼び出し画像を加工するコマンドです。

主な機能

  • 画像サイズの変更
  • 画像の回転
  • 画像のファイルフォーマット変換
  • 画像のトリミング、切り抜き
  • 各種グラフィックエフェクト
  • EXIFなどの画像情報表示

Download

Samples

Usage

Console

% resize.rb [options...] files

コンソールで resize.rb コマンドに下記のオプションをつけて実行することで、サイズ変更や各種エフェクト効果を適用した画像を生成し、ファイル名に -resize をつけて保存します。さらに、-f (または --format) オプションに png, jpg や gif などのフォーマットを指定することで、画像のフォーマット変換が可能です。変換対象のファイルが1つの時は、-o (または --output) オプションで出力ファイル名を変更できます。このとき、拡張子を変更することで出力フォーマットも同時に指定できます。また、デジカメで撮影した画像などに埋め込まれている EXIF などの情報を自動的に表示します(オプションを指定しなかった場合、情報表示だけで画像生成せずに終了します)。オプションは組み合わせて使用することができ、複数のファイルを指定することで同じ変換処理をまとめて適用することもできます。それぞれのオプションの具体的な使用例は上記 Samples を参照してください。

Options

-s or --scale number Specify the scaling factor of images in float. % resize.rb -s 0.1 image1.jpg image2.jpg # 1/10 scaled image -w or --width number Specify the width of resized images in pixel. (aspect ratio will be retained if not used with -h option) % resize.rb -w 100 image1.jpg image2.jpg -h or --height number Specify the height of resized images in pixel. (aspect ratio will be retained if not used with -w option) % resize.rb -h 50 image1.jpg image2.jpg -r or --rotate number Specify the rotation of images in degree. % resize.rb -r 90 image1.jpg image2.jpg # clockwise % resize.rb -r -90 image1.jpg image2.jpg # counter-clockwise -t or --trim number Specify the number of pixels to be trimmed from edges. If the number is 0, edges in the same color as corner pixels are removed (or made transparent if the number is negative). % resize.rb -t 10 image1.jpg image2.jpg % resize.rb -t 0 album.png # remove white edges % resize.rb -t -1 album.png # transparent edges -c or --crop "x,y,w,h" Specify the coordinates of upper left and size of a box to be cropped. % resize.rb -t 10,20,100,50 photo.jpg # 100x50 at (10, 20) -g or --grid "w,h:o" Specify the width and height of tiles and size of overlap pixels. % resize.rb -g 100,100:0 photo.jpg # 100x100 w/o overlap -e or --effect type Specify type of effect. (Available effects: "shadow", "reflect", "raise", "vignette", "sketch", "emboss", "negate", "flip", "flop" "gray", "sepia", "blur", "round", "roundedge") % resize.rb -e shadow image1.jpg image2.jpg # drop shadow -m or --watermark string Specify the label for watermark effect in string. % resize.rb -p "Kumamushi" image1.jpg image2.jpg -p or --polaroid string Specify the label for poraloid effect in string. % resize.rb -p "Kyoto trip" image1.jpg image2.jpg -f or --format type Specify the output file format. Useful for file format conversion and png output for some effects. (if not specified, file format is unchanged) % resize.rb -f gif image1.jpg image2.jpg # convert to gif % resize.rb -f png -p "my cat" -s 0.1 photo.jpg # polaroid in png -o or --output string Specify the output file name (and the file format by suffix). % resize.rb -o icon.png -s 0.1 image1.jpg # 1/10 image in png -q or --quiet Suppress verbose output (filenames and exif information).

なお、-e や -p などで指定できるエフェクトは ImageMagick 用で、GraphicsMagick では実装されていないため利用できないものも含まれます。先の Samples にあるような変換が実行できなかった場合は、下記 Install を参考に ImageMagick をインストールしてから、RMagick をコンパイル、インストールし直してみてください。

実行例

lunch photo.jpg ファイルを、1/10 のサイズに縮小しつつ、"today's lunch" というキャプションをつけたポラロイド写真風にし、PNG 画像に変換して photo-resize.png ファイルに保存する例。

% resize.rb -s 0.1 -p "today's lunch" -f png photo.jpg === Processing photo.jpg (1024x768) === exif:ColorSpace : 1 exif:CompressedBitsPerPixel : 3/1 exif:Contrast : 0 exif:CustomRendered : 0 exif:DateTime : 2007:05:08 12:42:33 exif:DateTimeDigitized : 2007:05:08 12:42:33 exif:DateTimeOriginal : 2007:05:08 12:42:33 exif:ExifImageLength : 2448 exif:ExifImageWidth : 3264 exif:ExifOffset : 192 exif:ExifVersion : 0221 exif:ExposureBiasValue : 0/1 exif:ExposureMode : 0 exif:ExposureProgram : 2 exif:ExposureTime : 1/40 exif:FNumber : 7/2 exif:Flash : 16 exif:FlashPixVersion : 0100 exif:FocalLength : 29/5 exif:ISOSpeedRatings : 400 exif:ImageDescription : exif:LightSource : 0 exif:Make : SONY exif:MaxApertureValue : 29/8 exif:MeteringMode : 5 exif:Model : DSC-T100 exif:ResolutionUnit : 2 exif:Saturation : 0 exif:SceneCaptureType : 0 exif:Sharpness : 0 exif:WhiteBalance : 0 exif:XResolution : 72/1 exif:YResolution : 72/1 jpeg:colorspace : 2 jpeg:sampling-factor : 2x2,1x1,1x1 >>> photo.jpg (1024x768) -> ./photo-resize.png (85x77)

Install

Mac OS X の場合

macports ImageMagick は Ghostscript とともに MacPorts を使ってインストールするのがラクでしょう。必要な各種グラフィックス関連ライブラリのインストールも自動的に行われます。RMagick は gem コマンドでインストールすることもできます。

% sudo port install ghostscript % sudo port install imagemagick % tar zxvf RMagick-1.15.6.tar.gz % cd RMagick-1.15.6 % ./configure % make % sudo make install

TODO

もう少し凝ったエフェクトを追加しますかね