下载安装下面三个代码库:
Pytesseract
Tesseract-OCR
Pillow
通过cmd输入pip install pytesseract进行安装,但是安装后并不能直接使用,还需要下载Tesseract-OCR。
- Python-tesseract是一个独立封装包;
- Python-tesseract功能是识别图片文件中文字,并作为返回参数返回识别结果;
- Python-tesseract默认支持tiff、bmp格式图片,只有在安装PIL之后,才能支持jpeg、gif、png等其他图片格式;
from PIL import Image
import pytesseract
image = Image.open('d:/temp/1.png')
result = pytesseract.image_to_string(image)
print(result)
执行过程中如果遇到如下错误:
C:\Users\28313\AppData\Local\Programs\Python\Python310\python.exe D:/pythonProject/testOCR.py
Traceback (most recent call last):
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 252, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\pythonProject\testOCR.py", line 4, in
result = pytesseract.image_to_string(image,lang='chi_sim')
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 413, in image_to_string
return {
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 416, in
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 284, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 256, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
Process finished with exit code 1
修改C:\Users\28313\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py 文件内容
#tesseract_cmd = 'tesseract' tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
C:\Users\28313\AppData\Local\Programs\Python\Python310\python.exe D:/pythonProject/testOCR.py
Don't believe the things you tell yourself so late: Clmarreln)
They could be your w
Process finished with exit code 0
从结果可以看出:本方法只能识别一些简单纯净的英文,中文、数字、字母和标点符号的效果还是不错的,如果是经过处理的图片,比如验证码等图片的识别,需要借助jTessBoxEditor训练字库才能提高识别的准确率哦!
如果需要在脚本中实现,如果引用selenium的一些方法
import os
import pytesseract
import time
import selenium
from PIL import Image, ImageEnhance
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
path = 'd:/tools/chromedriver.exe'
url = "https://www.baidu.com/"
driver = webdriver.Chrome(executable_path=path)
driver.get(url)
driver.save_screenshot('input.png')
inputText = driver.find_element(By.NAME,'wd')
inputText.send_keys("selenium")
left = inputText.location['x']+61
top = inputText.location['y']+1
right = inputText.location['x'] + inputText.size['width']+47
bottom = inputText.location['y'] + inputText.size['height']-1
image = Image.open('input.png').crop((left,top,right,bottom))
image = image.convert('L') #转换模式:L | RGB
image = ImageEnhance.Contrast(image) #增强对比度
image = image.enhance(2.0) #增加饱和度
image.save('input.png') #截图完成
code = pytesseract.image_to_string(image,lang='chi_sim')
print(code)