1

So here is the following image https://i.stack.imgur.com/I3Tl5.jpg

I want to isolate each one of these number so I can read them with pytesseract.

Here is the script I've wrote until now :

try:
    import Image
except ImportError:
    from PIL import Image
import pytesseract as tes

filename = "test.png";

x1 = (50,60,220,110)
x2 = (45,160,230,228)
x3 = (45,260,330,328)

image = Image.open( filename );

cropped_image = 'cropped_image'
scores = [x1, x2, x3]
for i in scores:
    cropped_image1 = image.crop ( i )
    results = tes.image_to_string(cropped_image1,lang="letsgodigital",boxes=False)
    print(results)

so basically I'm cropping this image for each numbers. However I don't feel like this solution is very efficient (plus it's not so easy to find the coordinate for each number. I'm planning to apply this script to multiple images but the number will always be at the same place. Any idea on how I can make this script better ?

Hope the issue is clear.

Thanks.

P.S : I don't know why but uploading the image directly in my post doesn't work....

Simon Breton
  • 2,638
  • 7
  • 50
  • 105

1 Answers1

1

If the numbers of each image are at the sample positions, use multiprocessing to process the cropped images would speed up the run time as multiprocessing could perform data parallelism to pass one image (data) per CPU core. If your machine has a single CPU with 4 cores, the processing time for your sample would be shorten to above 3-unit instead of 12-unit for 12 cropped images. You may have a google on it for more information and examples.

To improve the pytesseract OCR accuracy, you may threshold the image to black-in-white. Below is sample cropped images.

enter image description here

Sample code on black-in-white.

threshold = 60  
cropped = image.crop(x)
cropped = cropped.convert('L')
inverted = PIL.ImageOps.invert(cropped)
inverted_mask = inverted.point(lambda p: p < threshold and 255) 
thewaywewere
  • 8,128
  • 11
  • 41
  • 46
  • how then do you send the cropped image back to the ocr engine for reading after the extra preprocessing? – William Humphries Jul 06 '21 at 21:40
  • @WilliamHumphries `inverted_mask` is the processed image to be inputted to a OCR engine. You may refer to this [example](https://stackoverflow.com/questions/44619077/pytesseract-ocr-multiple-config-options/44632770#44632770) for some additional information. – thewaywewere Jul 08 '21 at 04:28