Stanislav Pankevich Stanislav Pankevich - 7 months ago 25
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

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

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.




UPDATE

Much has happened since I asked this question:


Answer

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'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.

http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use#ruby-vips

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").

http://rubyvips.holymonkey.com/classes/VIPS/Image.html#M000123

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:

#!/usr/bin/ruby

require 'rubygems'
require 'vips'
include VIPS

im = Image.new('/home/john/pics/theo.jpg')
im = im.lin [0, 1, 0], [0, 0, 0]
im.write('x.jpg')

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)
Comments