Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created January 31, 2013 13:18
Show Gist options
  • Save jcupitt/4682797 to your computer and use it in GitHub Desktop.
Save jcupitt/4682797 to your computer and use it in GitHub Desktop.
Find skin tones with ruby-vips.
#!/usr/bin/ruby
require 'rubygems'
require 'vips'
im = VIPS::Image.new(ARGV[0])
begin
# import to CIELAB with lcms
# if there's no profile there, we'll fall back to the thing below
cielab = im.icc_import_embedded(:relative)
rescue
# nope .. use the built-in converter instead
cielab = im.srgb_to_xyz().xyz_to_lab()
end
# convert to LCh
lch = cielab.lab_to_lch()
# get the h band
h = lch.extract_band(2)
# find all pixels with a hue in the range 60 - 90 (approximately skin tones)
mask = h.more(60).and(h.less(90))
mask.write(ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment