0

I need to get the text from image but in my image it only has one number it could be anything between 1-9 i need to get that number. i am using pytesseract to do this but on reading it shows empty text. the below is my image:

Single alphabet/number image

below is what i am trying:

from PIL import Image, ImageEnhance, ImageFilter
import pytesseract


def getText(image):
    image = Image.open(image)
    image.show()
    image = image.point(lambda x: 0 if x < 143 else 255)  # To clean Image
#     text = pytesseract.image_to_string(image).encode('utf-8').strip()
    text = pytesseract.image_to_string(image)
    return text


image1 = '/home/einfochips/Documents/Kroger_Automation_Framework/src/main/scripts/background.png'
txt1 = getText(image1)
print txt1, '_______________', type(txt1), len(txt1)
api55
  • 11,070
  • 4
  • 41
  • 57
Sachhya
  • 1,260
  • 1
  • 9
  • 16
  • using tesseract to read a single digit in a known font is complete overkill. use template matching – Piglet Apr 03 '18 at 12:57
  • may be you can try with resizing your image. – flamelite Apr 04 '18 at 04:42
  • @flamelite i tried but still i did not get. – Sachhya Apr 04 '18 at 05:02
  • @Sachhya i also tried, and i guess its because tesseract is considering single character as a graphic component. So you can try with other OCR library or You can try an alternate method which will work specially in this case, repeat the single character into same image, you will get OCR output consisting of multiple characters from there you can get your desired output. – flamelite Apr 04 '18 at 06:06
  • @flamelite thanks for your time, now i am going with other alternative. – Sachhya Apr 04 '18 at 06:20
  • @Sachhya can you explain your alternative? that might be helpful to me, – flamelite Apr 04 '18 at 06:22
  • @flamelite now i am not going with text reading what i am doing is i crop image initial and final with same coordinate from a large image(screenshot) and compare both crop images (Initial and final) if both are same than image not change (FAIL) if crop images not match that ( PASS ) – Sachhya Apr 04 '18 at 07:45

1 Answers1

0

You need to set your psm value. By default value is Mode 0 (I believe).

This worked for me text = pytesseract.image_to_string(Image.open(filename),config='--psm 10')