3

I have an image like this one: number

and I would like to have a black number written on white so that I can use an OCR to recognise it. How could I achieve that in Python?

Many thanks,

John.

user2040597
  • 469
  • 3
  • 8
  • 21
  • Are you just asking how to invert the image? Or do you also want to increase the contrast, sharpen edges, etc.? (Meanwhile, if you're looking for a way to defeat Captcha, that's not an easy thing to do—the whole point of Captcha is to make it hard for experienced crackers and spammers to defeat it, and there's a whole lot of complicated stuff you need to learn to even get started.) – abarnert Nov 29 '13 at 00:25
  • I try to read the numbers from my gaz meter that's all. In fact I will have sometimes flares as the numbers are behind a little glass. So I try to play with some tools to get the picture to an OCR. – user2040597 Nov 29 '13 at 00:29
  • You'll want to take a good quality image then, make sure you have sufficient lighting since that one you have seems poorly lit. – Lie Ryan Nov 29 '13 at 00:33

2 Answers2

5

You don't need to manipulate the image for OCR. For example, you could just use pytesser:

from PIL import Image
from pytesser import *
im = Image.open('wjNL6.jpg')
text = image_to_string(im)
print text

Output:

0
cyborg
  • 9,989
  • 4
  • 38
  • 56
3

If you just want to turn a white-on-black image to black-on-white, that's trivial; it's just invert:

from PIL import Image, ImageOps
img = Image.open('zero.jpg')
inverted = ImageOps.invert(img)
inverted.save('invzero.png')

If you also want to do some basic processing like increasing the contrast, see the other functions in the ImageOps module, like autocontrast. They're all pretty easy to use, but if you get stuck, you can always ask a new question. For more complex enhancements, look around the rest of PIL. ImageEnhance can be used to sharpen an image, ImageFilter can do edge detection and unsharp masking; etc. You may also want to change the format to greyscale (L8), or even black and white (L1); that's all in the Image.convert method.

Of course you have to know what processing you want to do. One thing you might want to try is playing around with the image in Photoshop or GIMP and keeping track of what operations you do, then looking for how to implement those operations in PIL. (It might be simpler to just use gimp-fu scripting in the first place instead of trying to use PIL…)

abarnert
  • 354,177
  • 51
  • 601
  • 671