如何使用Python从图像中提取文本?
OCR(光学字符识别)是将数字图像以电子方式转换为机器编码文本的过程。其中数字图像通常是包含类似于语言字符的区域的图像。 OCR 是模式识别、人工智能和计算机视觉的研究领域。这是因为新的 OCR 是通过提供样本数据来训练的,这些样本数据是通过机器学习算法运行的。这种从图像中提取文本的技术通常在可以确定图像包含文本数据的工作环境中进行。在本文中,我们将学习如何从图像中提取文本。我们将使用Python编程语言来这样做。
为了使我们的Python程序具有字符识别功能,我们将使用pytesseract OCR 库。通过在操作系统的命令解释器中执行以下命令,可以将该库安装到我们的Python环境中:-
pip install pytesseract
该库(如果在 Windows 操作系统上使用)还需要tesseract.exe二进制文件,以便正确安装该库。在上述可执行文件的安装过程中,系统会提示我们为其指定路径。需要记住此路径,因为稍后将在代码中使用它。对于大多数安装,路径为C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe。
解释:
首先我们从 PIL 库中导入 Image 模块(用于打开图像),然后从 pytesseract 库中导入 pytesseract 模块(用于文本提取)。然后在我们定义了path_to_tesseract变量之后,该变量包含我们在先决条件中安装的可执行二进制文件 ( tesseract.exe ) 的路径(此路径将取决于二进制文件的安装位置)。然后我们定义了包含图像文件路径的image_path变量。这个路径被传递给open()函数以从我们的图像中创建一个图像对象。在此之后,我们将存储在path_to_tesseract变量中的路径分配给pytesseract.tesseract_cmd变量(库将使用它来查找可执行文件并将其用于提取)。之后我们将图像对象( img )传递给image_to_string()函数。此函数将图像对象作为参数并返回其中识别的文本。最后,我们使用 text[:-1] 显示在图像中找到的文本(由于默认附加了一个附加字符(^L))。
示例 1:
演示图片:
下面是完整的实现:
Python3
from PIL import Image
from pytesseract import pytesseract
# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = r"csv\sample_text.png"
# Opening the image & storing it in an image object
img = Image.open(image_path)
# Providing the tesseract executable
# location to pytesseract library
pytesseract.tesseract_cmd = path_to_tesseract
# Passing the image object to image_to_string() function
# This function will extract the text from the image
text = pytesseract.image_to_string(img)
# Displaying the extracted text
print(text[:-1])
Python3
from PIL import Image
from pytesseract import pytesseract
# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = r"csv\d.jpg"
# Opening the image & storing it in an image object
img = Image.open(image_path)
# Providing the tesseract
# executable location to pytesseract library
pytesseract.tesseract_cmd = path_to_tesseract
# Passing the image object to
# image_to_string() function
# This function will
# extract the text from the image
text = pytesseract.image_to_string(img)
# Displaying the extracted text
print(text[:-1])
输出:
now children state should after above same long made such
point run take call together few being would walk give
示例 2:
演示图片:
代码:
蟒蛇3
from PIL import Image
from pytesseract import pytesseract
# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = r"csv\d.jpg"
# Opening the image & storing it in an image object
img = Image.open(image_path)
# Providing the tesseract
# executable location to pytesseract library
pytesseract.tesseract_cmd = path_to_tesseract
# Passing the image object to
# image_to_string() function
# This function will
# extract the text from the image
text = pytesseract.image_to_string(img)
# Displaying the extracted text
print(text[:-1])
输出:
Geeksforgeeks