[Image-SIG] Filtering out all but black pixels for OCR
Fredrik Lundh
fredrik at pythonware.com
Fri Jul 4 19:21:20 CEST 2008
Ned Batchelder wrote:
> 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")
if you want to avoid the clever lambda, you can use "point" instead,
with a precalculated lookup table:
# build lookup table (you can do this once, at the module level)
lut = [255] * 256 # make everything white
lut[0] = 0 # except things that are already pure black
...
better = img.point(lut) # for each image
</F>
More information about the Image-SIG
mailing list