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

Fredrik Lundh fredrik at pythonware.com
Tue Apr 28 18:21:12 CEST 2009

On Tue, Apr 28, 2009 at 4:13 PM, Eduardo Ismael <eismb at hotmail.com> wrote:
> Thanks, John - I'll have a look at numpy and vips (and CIELAB) right away.
> I'm having trouble thinking out of the "for pixel in image" loop. Is it
> possible to calculate with images and not pixels with PIL?

Sure - the trick is to figure out how to express your problem in terms
of the basic operations provided by the Image object itself, by the
ImageChops or ImageOps modules (which should be merged some day), etc.

And in 1.1.6 and later, there's also the ImageMath module.


To do a distance calculation, you could do something like:

    target = (red, green, blue)
    distance = 10

    # calculate a distance mask
    R, G, B = target
    r, g, b = im.split()
    distance3 = distance ** 3

    mask = ImageMath.eval(
        "convert(((abs(r - R)*abs(g - G)*abs(b - B)) < distance3)*255, 'L')",

    # paste in some nice color on pixels that are "close" to the target color
    im.paste("white", mask)

(no time to do benchmarking and detailed testing, but the above should
be fairly efficient)


More information about the Image-SIG mailing list