MojoMagick: Ruby Image Library (for ImageMagick) 8

Posted by science on January 30, 2008

Magick!

Science releases MojoMagick, a Ruby language image library “that does very little” ™. There are several useful image tools that “do a lot” - MojoMagick is designed to just a few things:

  • Be fast
  • Don’t leak memory
  • Make simple ImageMagick tasks, simple as Ruby tasks
  • Permit direct access to ImageMagick for complex tasks (i.e. get out of your way)
  • Provide a central access to ImageMagick for good code architecture
  • Easy to extend
  • Limit memory accessible to ImageMagick (where desired)

That’s what it does. It’s packaged as a Rails plugin, but does not require Rails to run. You can also install it as a gem.

You can download a copy by following the instructions here.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. weepy Tue, 12 Feb 2008 00:21:12 EST

    Looks very interesting. How does it differ from Rmagick/minimagick ?

    A gem would be a very good idea

  2. science Tue, 12 Feb 2008 10:43:24 EST

    RMagick hooks ImageMagick via DLL’s (last I looked). As a result it leaks memory like a sieve (last I looked). It’s fine for Ruby processes where the interpreter spins up, runs your code, and exits back to the OS. But it’s pretty bad in Rails, where the the interpreter spins up, and is re-entrant into the Rails stack over and over.

    Minimagick is very similar to MojoMagick in that they both use shell execution to accomplish their work. As a result, they don’t leak memory like RMagick. However, I have personal, anecdotal experience with Minimagick also leaking memory. I can’t say for sure, but that’s my unscientific experience.

    Most importantly, MiniMagick uses temp files to manipulate images. So if you want to get the size of an image in MiniMagick, the framework will copy your image to a temp file, and get the size of the temp file. If you want to resize an image in place, it will copy to a temp file, resize and copy back to the original. MojoMagick permits you to operate on a file directly and its built in functions (like resize and “get dimensions”) do this automatically for you.

    Significantly, MojoMagick supports easily limiting memory usage in ImageMagick. IM can use a surprising amount of memory and if you are in a limited RAM environment it can cause your core application to swap to disk which is Not Good.

    To limit memory usage in MojoMagick, be sure you’re upgraded to the current release of ImageMagick. Then you can use this code to examine and limit various ram components:

      # obtain/print a hash of current/default limits
      puts MojoMagick::get_default_limits.inspect
    
      # set/print limits on disk, area, map and ram usage
      MojoMagick::set_limits(:area => '32mb', :disk => '0', :memory => '64mb', :map => '32mb')
      puts MojoMagick::get_current_limits.inspect

    I’ll get to work on a gem soon!

  3. science Tue, 19 Feb 2008 11:09:23 EST

    Version 0.0.2 released: improved resource limits, error trapping

  4. al Fri, 29 Feb 2008 12:27:39 EST

    This is fantastic… saves me the hassle of hacking up minimagick to convince it to pass a command line.

  5. Will Mon, 30 Jun 2008 08:34:27 EDT

    A gem would be fantastic!

  6. science Mon, 30 Jun 2008 15:27:42 EDT

    Ok - Science may be lazy but does get the point. Gem created!

    MojoMagick version 0.1.0 GEM

    To install, save the above file to a temp folder. The run from that folder:

    gem install mojo_magick
    

    Remember that only works when you run the command from the folder where you d/l’ed the gem file!

    You can always browse for the latest Gem version here: http://trac.misuse.org/science/browser/mojo_magick/trunk/pkg I’ll try to update the Gem every time I time I push new code.

  7. weepy Mon, 30 Jun 2008 23:31:48 EDT

    awesome - nice work !

    Is there an obvious way to map an IM command to Mojo ?

  8. science Mon, 30 Jun 2008 23:32:41 EDT

    Yup that’s an important “feature” of Mojo - if you look in the “lib” folder you can see how I do it for the “resize” command for example:

    retval = MojoMagick::raw_command("convert", "\"#{source_file}\" -resize #{options[:width]}X#{options[:height]}#{scale_options} \”#{dest_file}\”")
    

    So basically you pass two params to “raw command”:

    #1 the actual IM command you wish to run as a string

    #2 the parameters (all in one string).

    At this point, it’s important that you pass the command separate from the parameters only if you are using the feature that lets you limit the amount of memory IM is going to use.. (that has to be put on the IM command line after the command but before the args).

Comments