Search code examples
pythonstringcomparisontesseractpython-tesseract

String comparison does not work in python


I'm writing a script that work with tesseract-ocr. I get text from screen and then I need to compare it with a string. The problem is that the comparison fails even if I'm sure that the strings are the same.

How can I made my code works?

Here my code:

import pyscreenshot as pss
import time
from pytesser import image_to_string

buy=str("VENDI")
buyNow=str("VENDI ADESSO")
if __name__ == '__main__':
    while 1:
        c=0

        time.sleep(2)
        image=pss.grab(bbox=(1104,422,(1104+206),(422+30)))
        text = str(image_to_string(im))
        print text
        if text==buy or text==buyNow:
            print 'ok'

For example as input:

Input image sample

And as output I get:

VENDI ADESSO

Which is the same string I need to compare, but during the execution I don't get ok on the console?


Solution

  • As it turns out, your string has new-lines (\n\n) at the end.

    You can use

    text = text.strip()
    

    to remove any surrounding whitespace from your string.

    For future cases, to check if your string has the content you think it has:

    print(repr(text))