String comparison does not work in python - 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:
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?

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.

Related

How to read this input? Trying to use sys.stdin.readline()

Trying to use this code to read an input for an exercise but not working
import sys
def get_string():
return sys.stdin.readlines()
inputs = get_string()
print(type(inputs))
The input is in this link:
https://pastebin.com/M5s9Svw8
when I execute this code the text seems to be corrupted in my terminal appears this text. One line in the middle seems different from the original.
https://pastebin.com/fb08qw4C
and it doesn't print the type of the file...

print() clears console with pytesseract

I followed a tutorial on a webpage to code a program to recognize text in images, the code it's very straightforward and it's the following.
import cv2, pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img = cv2.imread('test_image.jpeg')
text = pytesseract.image_to_string(img)
print(text)
print('done')
where "test_image.jpeg" is an image (with some text) placed in the same folder where the program has been saved.
I already installed cv2 and pytesseract and the installation was successful as they are in the list of modules installed.
But when I run the program, it clears the console (working on Spyder, Python 3.8) and it just prints 'done'.
I've made some checks and the problem is the line "print(text)", when it runs it, it clears the console.
I tried to use another output function, sys.stdout.write(text), and it does the same thing.
I tried to uninstall numpy and cv2 and reinstalled them.
I checked if text was a string with "print(type(text))" and it says string.
I checked in the Variable Explorer if just before printing the 'text' variable had the right text in it, and it had it, so everything in "background" should be ok.
Now I'm thinking I might just missing something easy that I can't see, could you help me? Thank you! :)
As noticed in the comment you can solve the problem printing repr(text)
print(repr(text))
You can notice that there are (at least in my case) a lot of \n and one \x0c, that is a sort of 'new page' escape sequence. These come from the way pytesseract works.
So the easier way to fix the problem I found is to delete them through
text = text[:-5]
The number of escape sequences depends on the image, but you could create a function to identify and delete them, for example, starting from the end, you delete every escape sequence you find.
Building on the previous answer by Matte, you can check for the existence of escape sequences by printing
print(repr(text))
To get rid of the escape sequence you have two options:
You strip the OCR result to remove all whitespace at the beginning or end of the string. This works regardless of how many \n or \x0c characters there are at the end.
text = text.strip()
You change or remove the page separator used by tesseract (by default this is \x0c, see https://tesseract-ocr.github.io/tessdoc/FAQ.html#tesseract-400).
text = pytesseract.image_to_string(img, config="-c page_separator=''")

How to encode to ascii,base64,hex from a file in python

In the following code, I am converting an image file into a string depending on the choices of radio buttons:
def convert_now(self):
self.img_data = ""
self.img_data_encoded = ""
file1 = open(self.filedict,'rb')
self.img_data = file1.read()
#RADIO_BUTTONS CHOICES, Convert to: 0-ascii, 1-base64, 2-Hex
v = self.rvar.get()
if v==0:
self.img_data_encoded=self.img_data
elif v==1:
self.img_data_encoded=base64.b64encode(self.img_data) (!)
elif v==2:
self.img_data_encoded=base64.b16encode(self.img_data) (!!)
I tried getting the base64 string from an image file using this line (!) and saved it to a "st" named string.
Then I tried getting the hex string using this one(!!)
The problem is when I compared the results I got from the code above to those I got from this website "https://www.branah.com/ascii-converter" when I used the "st"(the base64 string from the code)
they don't match at all.
Did I code something wrong ?
No, it doesn't seem like you did something wrong. Your code looks fine. Also, for me, both b64encode and b16encode produce the same output as the webpage you linked to. Here is an example.
>>> import base64
>>> base64.b64encode(b"test")
b'dGVzdA=='
>>> base64.b16encode(b"test")
b'74657374'
You can compare this to the results on the webpage. They match. So, there must be something else wrong. b64encode and b16encode work fine.

Python - Decode Text (German) from Hexadecimal Value and write it to a file

I know there are a lot of encoding/decoding topics here and I tried it for hours, but I'm still not able to solve it. Hence I want to raise a question:
I have a string with hexadecimal values, which in the end is a text that I want to write to a text file with the correct encoding.
hexvalues = "476572E2A47465646574656B746F72"
In the end, the (german) result should be "Gerätedetektor"
Currently I am using binascii.unhexlify() for the decoding, but it still doesn't show me the "ä" like its supposed to be, and instead, I get:
>> result = binascii.unhexlify(hexvalues)
Gerâ¤tedetektor
I tried to do result.decode("utf-8") and a lot of other things but either the script crashes or it also doesn't return what I want to see.
In the end, I want to write the word in the correct way to a file.
Any help would be highly appreciated!
Edit:
As I wrote before, I tried many things so it is kind of hard to give the ONE code that I'm using but here is an excerpt from the current version:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import binascii
resultfile = "text_GER.txt"
fpx = open(resultfile, 'wb')
hexvalues = "476572E2A47465646574656B746F72"
result= binascii.unhexlify(hexvalues )
result= result.decode("utf-8")
print(result)
fpx.write(result)
This one makes the script crash, but no further indicators why it does.
If i skip
result= result.decode("utf-8")
then my result print of result looks like this:
b'Ger\xe2\xa4tedetektor'

How to Print a Path in Python?

I want to make a script open the CMD and then enter a path:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\Users\XY\AppData\')
that doesn't work. So, I tried this:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\\Users\\huba5_000\\AppData\\')
However, this entered C:?Users?XY?AppData?
What I want it to enter is C:\Users\XY\AppData\. Do you know what I should write instead of '\\' ?
Thank you in advance!
when a string is read in from input() or from text boxes in gui's (in general.. idk about pag) the extra slashes are automatically put in. They are not automatically put in for string literals in your code however and must be escaped (hence the double slash). Here is a short console session (python 2.7) showing that functionality:
>>> s = raw_input('enter a path: ') #change raw_input to input() for python 3.x
enter a path: \usr\var
>>> s
'\\usr\\var'
>>> print s
\usr\var
notice when I entered the path, I did not escape my backslashes, yet when I call the internal representation of s they have been put in for me. when I want the output format, I call print to execute any formatting (escapes) contained within the string

Categories

Resources