使用 Python 从图像中获取文本的最佳方法是什么?

发布于 2025-01-15 06:34:40 字数 124 浏览 1 评论 0原文

我想从图像中获取文本。我尝试了 tesseract,但安装时遇到了问题,所以我想知道是否可以获得一些帮助或其他方法来做到这一点。 当我尝试使用 tesseract 时,它说我没有模块名称 PIL?但我知道我安装了枕头,我认为这是指它。

I want to get the text out of an image. I tried tesseract but I had issues installing it, so im wondering if I can get some help with that or another way to do it.
When I try to use tesseract it says I have no module names PIL? But I know I have pillow installed and i thought that was in reference to it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-01-22 06:34:40

我提供了一个 Colab 解决方案,因为这可能对大多数人有用。

在我们的 Colab 环境中安装 tesseract。

!sudo apt install tesseract-ocr
!pip install PyTesseract

导入库并安装我们的驱动器

from google.colab import drive
from google.colab.patches import cv2_imshow
drive.mount('/content/drive/')

设置我们的 pytesseract 路径,从我们的驱动器读取源图像,显示我们的源图像,然后最终将图像转换为文本。

import cv2
import pytesseract
import numpy as np

pytesseract.pytesseract.terreract_cmd = {
    r'/usr/bin/tesseract'
}

src = cv2.imread('/content/drive/MyDrive/Colab Notebooks/images/macbeth.png')
cv2_imshow(src)
output_txt = pytesseract.image_to_string(src)
print(type(output_txt))
print(output_txt)

输入图片此处描述

I've provided a Colab solution since that will probably be useful to the most people.

Install tesseract in our Colab environment.

!sudo apt install tesseract-ocr
!pip install PyTesseract

Import libraries and mount our Drive

from google.colab import drive
from google.colab.patches import cv2_imshow
drive.mount('/content/drive/')

Set our pytesseract path, read in source image from our Drive, show our source image, then finally convert the image to text.

import cv2
import pytesseract
import numpy as np

pytesseract.pytesseract.terreract_cmd = {
    r'/usr/bin/tesseract'
}

src = cv2.imread('/content/drive/MyDrive/Colab Notebooks/images/macbeth.png')
cv2_imshow(src)
output_txt = pytesseract.image_to_string(src)
print(type(output_txt))
print(output_txt)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文