[Image-SIG] Removing specific range of colors from scanned image

jcupitt at gmail.com jcupitt at gmail.com
Mon Apr 27 12:56:00 CEST 2009

2009/4/25 Eduardo Ismael <eismb at hotmail.com>:
> I scanned a color page from a book and I would like to remove specific
> colors from it.

The best solution is to calculate a colour difference metric. This is
rather like the 'magic wand' in most paint programs.

RGB isn't the best colour space for this, but it would sort-of work.
The idea is: imagine that your three colour values are coordinates in
a cube. You want to find pixels whose value is close to the position
of a point in the blue corner of this cube, in other words, something

   for pixel in image:
     if distance (image[pixel], blue) < 10:
       image[pixel] = white

The simplest distance function is just pythagoras, ie.

  def distance (a, b):
    vector = a - b
    return (vector.red ** 2 + vector.green ** 2 + vector.blue ** 2) ** 0.5

You can do this efficiently by processing at the image level rather
than by looping over pixels (always incredibly slow). So: calculate an
image where each pixel is the distance function for that point, then
threshold and use that mask to set sections of your original image to


More information about the Image-SIG mailing list