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.
Looks very interesting. How does it differ from Rmagick/minimagick ?
A gem would be a very good idea
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:
I’ll get to work on a gem soon!
Version 0.0.2 released: improved resource limits, error trapping
This is fantastic… saves me the hassle of hacking up minimagick to convince it to pass a command line.
A gem would be fantastic!
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:
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.
awesome - nice work !
Is there an obvious way to map an IM command to Mojo ?
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:
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).