i tried to open read the image using the pytesseract , however the code is not able to read it please check this photo im using for reading the text.
below is my code:-
import cv2
import time
import pyscreenshot as ImageGrab
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'C:/Users/RTam/AppData/Local/Programs/Tesseract-OCR/tesseract.exe'
def takescreenshot():
path= (r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos')
im=ImageGrab.grab(bbox=(900,1000,1200,1100))
im.save(path+'\\'+'ss.png')
img= cv2.imread(r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos\ss3.png')
cv2.imshow('sample',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
sample_text= pytesseract.image_to_string(img)
print(sample_text)
the only output im getting is and empty space please help
Eventually, I found the answer to my question.
However this code will not run properly in Spyder IDE, so we should make sure we have the latest tesseract version.
import cv2
import time
import pyscreenshot as ImageGrab
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'C:/Users/RTam/AppData/Local/Programs/Tesseract-OCR/tesseract.exe'
def takescreenshot():
path= (r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos')
im=ImageGrab.grab(bbox=(900,1000,1200,1100))
im.save(path+'\\'+'ss.png')
img= cv2.imread(r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos\ss3.png')
def clerify_pic():
img2 = cv2.resize(img, (0, 0), fx=2, fy=2)
gry = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
return pytesseract.image_to_string(thr)
Related
Trying to implement OCR into my simple python program, but Replit is being weird as usual with the module requirements. I get multiple errors, but the following shows first.
I tried to run this code:
import cv2
import pytesseract
import numpy as np
img = cv2.imread('/Users/marius/Desktop/jimdoo.png')
#Alternatively: can be skipped if you have a Blackwhite image
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
gray = cv2.bitwise_not(img_bin)
kernel = np.ones((2, 1), np.uint8)
img = cv2.erode(gray, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=1)
out_below = pytesseract.image_to_string(img)
print("OUTPUT:", out_below)
Not familiar with OCR so I can't MRE (sorry).
But this along with many other errors occured:
raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
Not very good at programming, what does this mean? Much thanks.
Tesseract works fine when I use other pictures but whenever I use this picture it doesn't recognize the picture.
Can someone explain me why please?
import cv2
import pytesseract
import time
import random
from pynput.keyboard import Controller
keyboard = Controller() # Create the controller
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread("capture5.png")
#img = cv2.resize(img, (300, 300))
cv2.imshow("capture5", img)
text = pytesseract.image_to_string(img)
print(text)
cv2.waitKey(0)
cv2.destroyAllWindows()
I fixed my problem, all I needed to do was add this code to my script.
text = pytesseract.image_to_string(
img, config=("-c tessedit"
"_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
" --psm 10"
" "))
In these lines of code, I create a small red image and save it as "new_image.png". But I can't find the saved image, where is it saved? And can I change the place where I want to save my image?
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
I tested your code:
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
It works well for me:
The reason may because your current work path is not as you thought.
See my answer here:
https://stackoverflow.com/a/66449241/12838403
If you don't specify a path, the image is saved in the same path as the python file generating the image
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
To save image in a directory you specify, you can use
from PIL import Image
import os
image_path = "path/to/image"
image = image.save(f"{image_path}/image.png")
Note: If this directory does not exist, you would have to create it before saving your image in it.
from PIL import Image
import os
image_path = "path/to/image"
os.mkdir(image_path)
image = image.save(f"{image_path}/image.png")
I am building a character identifier from an image using Tesseract and Python.
This is my code:
import cv2
import numpy as np
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
img = cv2.imread("bigsleep.jpg")
text = pytesseract.image_to_string(img)
cv2.imshow("Img", img)
cv2.waitKey(0)
print(text)
I am getting the following error while executing this program
TypeError: Unsupported image object
Can anyone solve this issue
I am trying to import an images dataset to test_image variable. I'd like to try this way because in my opinion it looks better. But I am getting this error:
(build-in function imread() returned NULL
import cv2 as cv
import os
test_images_path = './test_images/'
test_image = [cv.imread(os.path.join(test_images_path, image) for image in os.listdir(test_images_path))]
Closing parenthesis of cv.imread() was at the wrong place:
import cv2 as cv
import os
test_images_path = './test_images/'
test_images = [cv2.imread(os.path.join(test_images_path, image)) for image in os.listdir(test_images_path)]
for img in test_images:
cv2.imshow('img', img)
cv2.waitKey(0)
I think u have not clearly specified the path from where u are trying to fetch image is plz make sure u saved the image in same location where ur python file is saved