[Image-SIG] grayscale image: find darkest pixel and delete lighter ones

Fredrik Lundh fredrik at pythonware.com
Thu Apr 23 15:42:22 CEST 2009

On Thu, Apr 23, 2009 at 12:58 AM, Eduardo Ismael <eismb at hotmail.com> wrote:
> Hi!
> I wonder if it is possible to do something like the following with the
> Python Imaging library:
> for pixel in image:
>     get how close to black it is
> register what would be the darkest shade found in the image
> delete pixels lighter than the darkest.

to get the darkest color in the image, use:

    lo, hi = im.getextrema()

you can then create a lookup table that maps everything above lo plus
a small margin to white:

    lut = [0]*256 # all black
    for ix in range(lo + 10, 256):
        lut[ix] = 255 # make everything from lo+10 to 255 white

and use the "point" method to translate the whole image:

    im = im.point(lut)

tweak as necessary.


More information about the Image-SIG mailing list