1.1.环境配置
pip install Pillow
pip install pytesseract
安装tesseract-ocr-setup.exe
1.2.操作图片
操作图片:
from PIL import Image
# 打开图片
im = Image.open('test.png')
# 获取图片尺寸
w, h = im.size
# 获取图片源格式
im.format
# 获取图片模式
im.mode(L:灰度图像,RGB:真彩色,CMYK:pre-press图像)
# 转换为灰度图像
im.convert('L')
# 重设图片大小
im.thumbnail((w // 4, h // 4))
# 截取图片
im.crop((x, y, x + w, y + h))
# 显示图片
im.show()
# 保存图片
im.save('text2.png')
1.3.读取图片内容
text = pytesseract.image_to_string(im)
1.4.FAQ
1.4.1.报错
报错信息如下:
Traceback (most recent call last):
File "E:/ste_test/automation_1/my_test/pictures/test.py", line 113, in <module>
test4()
File "E:/ste_test/automation_1/my_test/pictures/test.py", line 107, in test4
text = pytesseract.image_to_string(im, config='-psm 10')
File "C:\Users\xiaoyang.huang\AppData\Local\Programs\Python\Python36\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Users\xiaoyang.huang\AppData\Local\Programs\Python\Python36\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Users\xiaoyang.huang\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\xiaoyang.huang\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
解决方法:
修改pytesseract.py文件中的内容:
修改前:
tesseract_cmd = 'tesseract'
修改后:
tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\\tesseract'
1.4.2.不能识别单个字符
问题描述:直接使用pytesseract.image_to_string(im)不能读取单个字符
解决方法:
添加参数:config=’-psm 10’
text = pytesseract.image_to_string(im, config='-psm 10')
其他参数:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.