Skip to content
stanislaw edited this page Jun 22, 2012 · 27 revisions

Examples

Color given jpg file to green (this is my favourite example).

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, 0, 0], [0, 255, 0]

im.write 'output.jpg'

Constant image

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'

Gamma

#!/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' 
Clone this wiki locally