首页 > 解决方案 > cv2 直接进行 tesseract 不保存

问题描述

import pytesseract
from pdf2image import convert_from_path, convert_from_bytes
import cv2,numpy
def pil_to_cv2(image):
    open_cv_image = numpy.array(image)
    return open_cv_image[:, :, ::-1].copy() 


path='OriginalsFile.pdf'
images = convert_from_path(path)
cv_h=[pil_to_cv2(i) for i in images]
img_header = cv_h[0][:160,:]
#print(pytesseract.image_to_string(Image.open('test.png'))) I only found this in tesseract docs

你好,有没有办法img_header直接使用pytesseract读取而不保存,

pytesseract 文档

标签: pythonimagepython-tesseract

解决方案


pytesseract.image_to_string() 输入格式

正如文档所解释pytesseract.image_to_string()的,需要一个 PIL 图像作为输入。因此,您可以轻松地将您的 CV 图像转换为 PIL 图像,如下所示:

from PIL import Image
... (your code)
print(pytesseract.image_to_string(Image.fromarray(img_header)))

如果你真的不想使用 PIL!

见: https ://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py

pytesseract 是运行 tesseract 命令def run_and_get_output() 行的简单包装器,您会看到它将图像保存到临时文件中,然后将地址提供给 tesseract 以运行。

因此,您可以对 opencv 执行相同的操作,只需重写 pytesseract only.py文件以使用 opencv 执行此操作;我没有看到任何性能改进。


推荐阅读