Python,pytesseract未识别图像

1 投票
1 回答
39 浏览
提问于 2025-04-12 16:52

我有一段简单的代码

可执行文件的路径是对的,截图的路径也是对的,那里有一张图片

screenshot.jpg

为什么它无法识别文本,即使这些文本并不是难以辨认的呢?

我尝试过使用图像增强、阈值处理和转换为灰度图像
我还尝试过用.png格式,但还是不行
这到底是为什么呢?

import pytesseract as pt
from PIL import Image

pt.pytesseract.tesseract_cmd = 'D:\\tesseract\\tesseract.exe'

def extract_text_from_image(image_path):

    img = Image.open(image_path)

    text = pt.image_to_string(img)
    
    return text

image_path = 'D:\\script\\materials\\screenshot.jpg'
text = extract_text_from_image(image_path)
print(text)

1 个回答

1

我通过将页面分割模式设置为6,成功从你的图片中读取了文字。

pt.image_to_string(img, config="--psm 6")

你可以在这里了解不同的分割模式以及它们的作用: https://pyimagesearch.com/2021/11/15/tesseract-page-segmentation-modes-psms-explained-how-to-improve-your-ocr-accuracy/

撰写回答