I am the one who deleted the answer.
Edit
After the situation settled down I would like to comment on few things:
1) After reading comments from senior users and moderators I came to conclusion that what I did was wrong, It was breaking community rules and as a result I will try to handle similar solution in the lines of SO rules and more calmly.
2) My reaction was exaggerated. However, the behaviour of author of that OP was for me unacceptable and offended me. This OP from the same author also offended me as author described the situation in his favor and not quite as it happened. Author of that OP now accepted answer which is same as mine, but that answer does not even answers OP's sub-questions. Its quite clear now that the topic of this OP 'Told I would only get correct answer if I accepted incorrect one' is not really what this post was about.
I asked a question, and was given an answer that dealt with an image taken off a website
The problem was that @hegash had a problem with getting any text with pytesseract during recognition on a particular image which he posted with the question. I provided a script, which downloaded an image, the one he posted with the question. That is exactly the one he couldn't recognize, and step by step did image processing with use of pytesseract to recognize the text. The result of the code I posted was exactly what was on the image he posted. Besides the script I provided information how to increase recognition.
- I deleted the answer, because he said it didn't work, because his original image was on a PC, and I didn't provide information how to load image.
Seriously, he doesn't know how to load image in Python, and he wants to work with pytesseract? But OK. How many Stack Overflow questions deal with loading images into a Python script? Billions, so why doesn't he search first? His question was about helping to recognize the image, not load an image from a PC.
That was a moment I realized that my answer will not be accepted until I provide exact code for his exact problem and to be honest, I don't deal with people who go to Stack Overflow for getting a solution done by others instead of getting actual help.
Stack Overflow is business for me. I take from it and I give to it. It's not charity.
This was my answer to his question (link):
There are several reasons:
Edges are not sharp and continuous (By sharp I mean smooth, not with teeth)
Image is too small, you need to resize
Font is missing (not mandatory, but a trained font incredibly improve possibility of recognition)
Based on points 1) and 2) I was able to recognize text.
1) I resized image 3x and 2) I blurred the image to make edges smooth
import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
from PIL import Image
def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
url = 'https://i.stack.imgur.com/J2ojU.png'
img = url_to_image(url)
retval, img = cv2.threshold(img,200,255, cv2.THRESH_BINARY)
img = cv2.resize(img,(0,0),fx=3,fy=3)
img = cv2.GaussianBlur(img,(11,11),0)
img = cv2.medianBlur(img,9)
cv2.imshow('asd',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
txt = pytesseract.image_to_string(img)
print('recognition:', txt)
>> recognition: Gm
Note:
This script is good for testing any image on web
Note 2:
All processing is based on your posted image
Note 3:
Text recognition is not easy. Every recognition requires special processing. If you try this steps with different image, it may not work at all. Important is to try a lot of recognition on images so you understand what tesseract wants
After this, he asked:
How would I do this for an image stored on my computer? – hegash 16 hours ago
I answered:
Well first select it as answer and then we can go to your new question – Martin
... New question, new answer. That's how I see it
When I write, it may look unfriendly and impolite, but I am fair. If someone needs help, I try to help him/her. But I don't help people that doesn't want help, but instead they want a slave who codes for them.
Image.open(IMAGE FILE)
. I get that that's not the same as spelling out that a local file is required, but the least an answer could do was follow that instead of... trying to load an image off of an i.stack.imgur.com URL? This just brings me back to my age-old rant of "Why provide code in a question if it's just going to get ignored?" – BoltClock♦ Jan 20 '19 at 12:46