Stanislav Pankevich Stanislav Pankevich - 5 months ago 14
Ruby Question

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

I am completely new to image processing. I know nothing about what is JPEG internally and how it works.

I wonder, if I can find somewhere piece of ruby code performing following simple operation:

  1. Open jpeg file.

  2. Iterate through each pixel and set it's color to fx green.

  3. Write result to another file.

I am especially interested in how this can be accomplished using ruby-vips library

My goal - to learn how to perform basic image processing operations using ruby-vips (gamma correction, brightness, tint, ...)

Any links to working examples more complex than 'hello world'-like one on ruby-vips's github page would be highly appreciated!

If there are alternatives to ruby-vips, I would be thankful for them too.


Much has happened since I asked this question:


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:

There's another ruby-vips example on the main vips site, on the benchmarks page. This example loads an image, crops 100 pixels off every edge, downsizes by 10%, sharpens and saves again.

To set the red and blue channels to zero and just leave a green image (is this what you mean?) you might multiply R and B by zero and G by 1. A handy operation for this is "lin", (meaning "linear transform").

out = in.lin(a, b)

sets every pixel in out to be

out = in * a + b

It lets you give an array of numbers for a and b and will use one array element per image channel. So therefore:


require 'rubygems'
require 'vips'
include VIPS

im ='/home/john/pics/theo.jpg')
im = im.lin [0, 1, 0], [0, 0, 0]

To change image gamma you might try something like:

im = im.pow(0.5).lin(255 / 255 ** 0.5, 0)

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 = Image.identity(1)
lut = lut.pow(0.5).lin(255 /255 ** 0.5, 0)
im = im.maplut(lut)