1、安装所需要的库
pip install pytesseract pillow
2、准备图像
from PIL import Image
# 加载图像
image_path = "path/to/your/image.png"
image = Image.open(image_path)
3、进行OCR识别
import pytesseract
# pytesseract.pytesseract.tesseract_cmd = r'C:/tesseract.exe'
data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
for i in range(len(data['text'])):
if int(data['conf'][i]) > 0:
text = data['text'][i]
x = data['left'][i]
y = data['top'][i]
w = data['width'][i]
h = data['height'][i]
print(f'识别的文本:{text} 位置:(x: {x}, y: {y}, w: {w}, h: {h})')