Mike Meisner schrieb:
I'd like to use PIL to prep an image file to improve OCR quality.
Specifically, I need to filter out all but black pixels from the image
(i.e., convert all non-black pixels to white while retaining the black
pixels).
You could do something like the following:
from PIL import Image
img = Image.open("sample.png")
(xdim, ydim) = img.size
# this assumes that no alpha-channel is set
black = (0, 0, 0)
white = (255, 255, 255)
if Image.VERSION >= "1.1.6":
data = img.load()
for y in range(ydim-1, 0, -1):
for x in range(xdim):
if data[x,y] != black:
data[x,y] = white
else:
data = img.getdata()
for y in range(ydim-1, 0, -1):
for x in range(xdim):
if data[x+y*xdim] != black:
data[x+y*xdim] = white
img.save("sample-filtered.png")
_______________________________________________
Image-SIG maillist - Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig