Help us understand the problem. What is going on with this article?

画像の加工処理にMiniMagickを使う

More than 5 years have passed since last update.

MiniMagick
https://github.com/minimagick/minimagick

A ruby wrapper for ImageMagick or GraphicsMagick command line.

ReadmeにはRMagickのメモリリークについての言及があり、そのsolutionとして作ったとの記述が。
これを使うことにしました。

MiniMagickを使う

mini_magick.rb
require 'mini_magick'
image = MiniMagick::Image.open("food.jpeg")
result = image.composite(MiniMagick::Image.open("overlay.png", "jpg")) do |c|
  c.gravity "northeast" 
  c.quality "75" 
end

result.write "output_minimagick.jpg" 

↓imagemagickのCLIを使う場合

imagemagick.rb
cmd = "convert food.jpeg overlay.png -gravity northeast -composite -format jpg -quality 75 output_imagemagick.jpg" 
system cmd

実行速度測定

(480x480の画像にオーバーレイ処理した場合)

$ time ruby mini_magick.rb
real 0m0.438s
user 0m0.288s
sys 0m0.117s

$ time ruby imagemagick.rb
real 0m0.146s
user 0m0.081s
sys 0m0.054s

MiniMagickを使うと、CLIを直接使う場合の約3倍の処理時間がかかりました。
速度を考えるとCLIが最強ですが、MiniMagickを使えば前述のコードのように、CLIを直接呼び出すよりも書きやすくなります。

上記のコードは以下にもアップしてあります。
https://github.com/masawo/minimagick_test

参考

ImageMagick: Command-line Tools
http://www.imagemagick.org/script/command-line-tools.php

mini_magickとImageMagickで画像を切り取る - 橋本詳解
http://d.hatena.ne.jp/shokai/20120303/1330783040

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした