可以使用Python中的OCR技术来实现图片提取文字。OCR(Optical Character Recognition)光学字符识别技术,可以识别图像中的文字并将其转换为文本。常用的Python OCR库包括pytesseract、opencv-python等。
以下是一个简单的示例代码,使用pytesseract库实现图片文字提取:
import pytesseract
from PIL import Image
# 打开图片文件
image = Image.open('image.png')
# 使用pytesseract库提取文字
text = pytesseract.image_to_string(image, lang='chi_sim+eng')
print(text)
其中,'image.png’为要提取文字的图片文件名,lang='eng’指定使用英语语言进行识别。如果需要识别其他语言,则需要下载对应的语言包并进行配置。
安装方法:
Ubuntu
You can install Tesseract and its developer tools on Ubuntu by simply running:
pip install pytesseract
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
sudo apt install tesseract-ocr-chi-sim
Note for Ubuntu users: In case apt is unable to find the package try adding universe entry to the sources.list file as shown below.
sudo vi /etc/apt/sources.list
Copy the first line “deb http://archive.ubuntu.com/ubuntu bionic main” and paste it as shown below on the next line.
If you are using a different release of ubuntu, then replace bionic with the respective release name.
deb http://archive.ubuntu.com/ubuntu bionic universe