【发布时间】:2017-10-10 04:08:34
【问题描述】:
我要做的是用 pyautogui 截取一个数字,然后用 pytesseract 将数字转换为字符串。代码: 导入pyautogui 进口时间 进口 PIL 从 PIL 导入图像 导入pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)//Tesseract-OCR//tesseract'
# Create image
time.sleep(5)
image = pyautogui.screenshot('projects/output.png', region=(1608, 314, 57, 41))
# Resize image
basewidth = 2000
img = Image.open('projects/output.png')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
img.save('projects/output.png')
col = Image.open('projects/output.png')
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save('projects/output.png')
# Image to string
screen = Image.open('projects/output.png')
print(pytesseract.image_to_string(screen, config='tessedit_char_whitelist=0123456789'))
现在看来 pytesseract 不接受 pyautogui 创建的屏幕截图。代码运行良好,没有问题,但打印一个空字符串。但是,如果我在绘画中创建图像,并将其作为“output.png”保存到正确的文件夹中,就像截屏一样,它确实可以工作。
Image output after resize and adjustments
有人知道我遗漏了什么吗?
【问题讨论】:
标签: python screenshot tesseract pyautogui pytesser