【发布时间】:2023-01-31 22:28:24
【问题描述】:
我正在尝试读取屏幕上的数字,为此我正在使用 pytesseract。问题是,尽管它有效,但它工作缓慢并且根本没有给出好的结果。例如,这张图片:
我可以制作这个阈值图像:
它读取 5852 而不是 585,这是可以理解的,但有时使用不同的阈值可能会更糟。例如,它可以将 1 000 000 读取为 1 aaa eee,或者将 585 读取为 5385r(是的,它甚至可以无缘无故地添加字符)
没有任何方法可以强制 pytesseract 只读取数字或简单地使用比 pytesseract 更好用的东西吗?
我的代码:
from PIL import Image
from pytesseract import pytesseract as pyt
import test
pyt.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
def tti2(location) :
image_file = location
im = Image.open(image_file)
text = pyt.image_to_string(im)
print(text)
for character in "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ*^&\n" :
text = text.replace(character, "")
return text
test.th("C:\\Users\\Utilisateur\\Pictures\\greenshot\\flea market sniper\\TEST.png")
print(tti2("C:\\Users\\Utilisateur\\Pictures\\greenshot\\flea market sniper\\TESTbis.png"))
“测试”代码(用于阈值):
import cv2
from PIL import Image
def th(Path) :
img = cv2.imread(Path)
# If your image is not already grayscale :
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold = 60 # to be determined
_, img_binarized = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
pil_img = Image.fromarray(img_binarized)
Path = Path.replace(".png","")
pil_img.save(Path+"bis.png")
【问题讨论】:
标签: python automation python-tesseract