Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created January 31, 2013 13:05
Show Gist options
  • Save jcupitt/4682718 to your computer and use it in GitHub Desktop.
Save jcupitt/4682718 to your computer and use it in GitHub Desktop.
detect skin tones in an image with libvips and python
#!/usr/bin/python
import sys
from vipsCC import *
im = VImage.VImage(sys.argv[1])
try:
# import to CIELAB with lcms
# 1 means relative colorimetry
# this assumes there is a profile there, of course
cielab = im.icc_import_embedded(1)
except:
# nope .. use the built-in converter instead
cielab = im.sRGB2XYZ().XYZ2Lab()
# convert to LCh
lch = cielab.Lab2LCh()
# 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).andimage(h.less(90))
mask.write(sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment