怎么提取图片上的文字

知识问答 2025-09-03 19:56:01 来源:互联网

要提取图片上的文字,可以使用光学字符识别(OCR)技术,OCR 是一种将图片中的文字转换为可编辑文本的计算机视觉技术,有许多 OCR 工具和库可供选择,如 Tesseract、Adobe Acrobat、百度OCR 等,以下是使用 Tesseract OCR 提取图片文字的简单步骤:

1、安装 Tesseract OCR:访问 https://github.com/tesseract-ocr/tesseract 下载并安装适合您操作系统的 Tesseract OCR。

2、安装 Python 的 pytesseract 库:在命令行中运行pip install pytesseract 以安装 pytesseract 库。

3、安装 Pillow 库:在命令行中运行pip install Pillow 以安装 Pillow 库,它包含处理图像的功能。

4、编写代码:使用以下代码将图片中的文字提取为文本。

from PIL import Imageimport pytesseractdef extract_text_from_image(image_path):    image = Image.open(image_path)    text = pytesseract.image_to_string(image, lang='chi_sim')    return textimage_path = 'path/to/your/image.jpg'text = extract_text_from_image(image_path)print(text)

5、运行代码:将path/to/your/image.jpg 替换为您要提取文字的图片的路径,然后运行代码,代码将打印出图片中的文字。