有时候需要复制图片上的文字,一个个敲起来还是太麻烦了,用python脚本转换成长文字粘贴,很实用。
安装环境依赖,以ubuntu系统为例:
一. 安装 tesseract-ocr 包
sudo apt-get install tesseract-ocr
二. 安装 pytesseract
pip install pytesseract
三. 安装中文字库
sudo apt-get install tesseract-ocr-chi-sim
四.代码
# -*- coding: UTF-8 -*-
from PIL import Image
import pytesseract
# 识别带中文(包括英文数字都可识别)
text = pytesseract.image_to_string(Image.open('/sfile/img_tmp.png'), lang='chi_sim')
print(text)
print('==========================\n\n')
# 识别英文(不能识别中文)
text = pytesseract.image_to_string(Image.open('/sfile/0403.png'))
print(text)