Pytesseract 仅在某些文件上找不到任何文本

Pytesseract doesn't find any text only on some files

我有以下代码,问题是,在某些图像上,return 值为空。图像的结构总是相同的。它是白色背景上的纯黑色文本。清晰可读。 50%的结果是优秀的,其他的都是空的。

我得到的唯一错误是:

wand/image.py:4623: CoderWarning: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `filename.png' @ warning/png.c/MagickPNGWarningHandler/1747 self.raise_exception()

但它每次都会引发此错误,即使输出正常也是如此。

def retrievetext(self,docname):
    r = BytesIO()
    self.ftp.retrbinary("RETR /httpdocs/"+docname , r.write )  
    r.seek(0)
    with wi(file=r, resolution = 400) as pdf:
        pdfImage = pdf.convert('png')

    imageBlobs = []

    for img in pdfImage.sequence:

        imgPage = wi(image = img)
        imgPage.crop(left=200,top=600,width=1800,height=800)
        imageBlobs.append(imgPage.make_blob('png'))

    recognized_text = []
    for imgBlob in imageBlobs:
        im = Image.open(BytesIO(imgBlob))
        im = im.convert('L')
        text = pytesseract.image_to_string(im, lang = 'deu')
        recognized_text.append(text)

   return recognized_text

有人知道如何改进结果吗?

此致

您的某些图像处于灰度模式。所以在发送到pytesseract之前需要先将它们转换成RGBA格式:

img = Image.open('example2.png')
rgbimg = Image.new('RGBA', img.size)
rgbimg.paste(img)
text = pytesseract.image_to_string(rgbimg, lang='deu')
print(text)