Ruby/Rails Image Processing Libraries

Ruby/Rails image processing libraries

I wrote something like the following:

require 'rubygems'
require 'RMagick'
include Magick

image = Image.new(50, 50) {
self.background_color = "white"
}
text = Draw.new
text.annotate(image, 0,0,0,40, 'Named Colors') {
self.fill = 'black'
self.pointsize = 32
}
image.write("image.png")

Which should be easy enough to follow. Also have a look at the documentation. Whilst it's not quite laid out to perfection, it's pretty much all there.

Ruby-vips image processing library. Are there any good examples of usage?

update ruby-vips has changed a bit since this answer was written. I've revised it for the current (2018) version.

I'm one of the maintainers of libvips, the image processing library that ruby-vips wraps.

Tim's ruby-vips repository hasn't been touched for a while. I have a fork here that works with current libvips:

https://github.com/jcupitt/ruby-vips

There are some examples here:

https://github.com/jcupitt/ruby-vips/tree/master/example

To set the red and blue channels to zero and just leave a
green image you might multiply R and B by
zero and G by 1. ruby-vips uses arrays to represent pixel constants, so you can just write:

out = in * [0, 1, 0]

A complete runnable example might be:

#!/usr/bin/ruby

require 'vips'

im = Vips::Image.new_from_file '/home/john/pics/theo.jpg'
im *= [0, 1, 0]
im.write_to_file 'x.jpg'

There's a trick you can use for new_from_file: if you know you will just be doing simple top-to-bottom operations on the image, like arithmetic or filtering or resize, you can tell ruby-vips that you only need sequential access to pixels:

im = Vips::Image.new_from_file '/home/john/pics/theo.jpg', access: :sequential

Now ruby-vips will stream your image. It'll run the load, the multiply and the save all in parallel and never keep more than a few scanlines of pixels in memory at any one time. This can give a really nice improvement to speed and memory use.

To change image gamma you might try something like:

im = im ** 0.5 * 255 / 255 ** 0.5

Though that'll be a bit slow (it'll call pow() three times for each pixel), it'd be much faster to make a lookup table, run the pow() on that, then map the image through the table:

lut = Vips::Image.identity
lut = lut ** 0.5 * 255 /255 ** 0.5
im = im.maplut lut

Any questions, please feel free to open them on the rubyvips issue tracker:

https://github.com/jcupitt/ruby-vips/issues

Image processing libraries

Check out the python Scipy library. It's an open source fast n-dimensional array manipulation library in python. It has all the basic image processing tools:

  • linear and rank filters
  • FFT, convolution
  • morphological operation

It doesn't have all the more advanced functions of Matlab's Image Processing Toolbox. However, as Vereb suggested, a lot of those can be found in ITK (also available in python flavor).

Like you I wanted to get away from matlab to a dynamic language like python. And like you, I was disappointed by PIL, when I realized it was just an ImageMagick in python. You'll still need to use PIL for reading/writing images.

To get a Matlab-like image processing experience with python get Numpy/Scipy, Matplotlib and Spyder. All of which and more is conveniently packaged in Python(X,Y) for windows only.

Why do most image processing gems for ruby require more then just a gem?

For your example of RMagick and ImageMagick, each one focuses on a separate responsibility. First, ImageMagick is a (vast) library of image manipulation capabilities. Resizing is one teeny portion. I assume you've looked it up and seen all the things it can do. Second, RMagick is a Ruby-specific front end to simplify using ImageMagick specifically for Ruby programmers. There will be other similar libraries for other langauges.

Why are they separate? Image manipulation requires oodles of specialized code which needs to process as efficiently as possible (speed, memory size, etc.). Such libraries tend to be written in C, or various flavors thereof. While it is not difficult to install ImageMagick, and write your own Ruby front end for it, it's not something most people want to do. Instead they hope someone else has already done it (RMagick), and they use just that.

Me, since most projects use so little of ImageMagick, I prefer to write my own front end for the ImageMagick commands I actually use, and with APIs dedicated to the processes I use the most often. I wouldn't suggest everyone do that, but it can be done.

Many gems are going to specialize in one focused purpose which solves a particular missing piece to a puzzle as they see it. If someone has already written a well-tested library X, and someone else has written Z, then if you need Y to glue them together in some way, you're likely to use X, use Z, and just write the Y piece. If you think Y might be useful to someone else, you can publish Y as a gem.

Another guy comes along, and wants Y, and finds out he needs X and Z too. Normal.



Related Topics



Leave a reply



Submit