-
Notifications
You must be signed in to change notification settings - Fork 61
Examples
You never iterate yourself through pixels one by one, it'd be much too slow.
Instead, imagine images are huge arrays of numbers and you have a library which can efficiently apply operations to those large matrices. Imagine what calculation the thing you want to do might correspond to, and just type it.
For 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. A handy vips 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:
#!/usr/bin/ruby
require 'rubygems'
require 'vips'
include VIPS
im = Image.new 'mypic.jpg'
im = im.lin [0, 1, 0], [0, 0, 0]
im.write 'output.jpg'
To make a constant image (where all pixels have the same constant value), make a 1x1 pixel zero image with Image::black, add some amount to it with .lin(), and expand it up to the size you need with .embed().
#!/usr/bin/ruby
require 'rubygems'
require 'vips'
include VIPS
im = Image.black 1, 1, 1
im = im.lin [0, 0, 0], [0, 0, 255]
im = im.embed :extend, 0, 0, 400, 400
im.write 'constant_blue.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 (see Basic concepts), 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)
Finally:
#!/usr/bin/ruby
require 'rubygems'
require 'vips'
include VIPS
im = Image.new 'mypic.jpg'
lut = Image.identity 1
gamma = 0.8
lut = lut.pow(gamma).lin(255 /255 ** gamma, 0)
im = im.maplut lut
im.write 'x.jpg'