python怎么提取图片文字

43次阅读
没有评论

共计 681 个字符,预计需要花费 2 分钟才能阅读完成。

要提取图片中的文字,可以使用 Python 的 OCR(Optical Character Recognition,光学字符识别)库。以下是使用 tesseract 库进行图片文字提取的示例代码:

  1. 首先,确保已经安装了 tesseract OCR 库和 Python 的 tesseract 库。可以使用以下命令进行安装:
pip install tesseract
pip install pytesseract
  1. 导入所需的库:
import pytesseract
from PIL import Image
  1. 加载并预处理图片:
image = Image.open('image.jpg')  # 加载图片
image = image.convert('L')  # 转为灰度图像
  1. 使用 tesseract 进行文字提取:
text = pytesseract.image_to_string(image)
print(text)

运行以上代码后,将会输出提取到的图片文字。

需要注意的是,以上代码使用的是 tesseract 的默认语言模型进行文字提取。如果需要提取特定语言的文字,可以使用 pytesseract.image_to_string 函数的 lang 参数指定语言模型,例如:

text = pytesseract.image_to_string(image, lang='chi_sim')  # 提取中文文字

此外,要使用 tesseract 进行文字提取,还需要将 tesseract 的可执行程序路径添加到系统环境变量中,或者在代码中指定 tesseract 的可执行程序路径。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-13发表,共计681字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)