[Image-SIG] Filtering out all but black pixels for OCR

Karsten Hiddemann karsten.hiddemann at mathematik.uni-dortmund.de
Wed Jul 2 13:47:45 CEST 2008

Ned Batchelder schrieb:
> If your image is single-channel (mode "L"), then you can use the eval 
> function:
>    img = Image.open("onechannel.png")
>    # at each pixel, if it isn't zero, make it 255..
>    better = Image.eval(img, lambda p: 255 * (int(p != 0)))
>    better.save("bilevel.png")

Ok, that's probably faster.

In my code, instead of doing "y in range(ydim-1, 0, -1)" you could use 
"y in range(ydim)" instead, too, it was just taken from a program of 
mine where I needed to invert the orientation of the image.

Fredrik has written something about the performance on pixel access with 
some timings here: http://effbot.org/zone/pil-pixel-access.htm


More information about the Image-SIG mailing list