【问题标题】:Image_to_string not reading text from tiff or tif files using pytesseractImage_to_string 不使用 pytesseract 从 tiff 或 tif 文件中读取文本
【发布时间】:2018-09-20 17:48:23
【问题描述】:

我正在尝试从 tif 或 tiff 图像文件中读取文本。这些文件有多个页面。

当我打印数组时,我只得到 true,然后没有文本。但是,当我使用 .png 文件时,我可以打印文本。

下面是我的代码。

from PIL import Image, ImageSequence
import pytesseract
from pytesseract import image_to_string
import numpy as np
import cv2
test = Image.open(r'C:\Python\BG36820V1.tiff')
#test1 = Image.open(r'C:\Users\Documents\declaration.png')
testarray = np.array(test)
print(testarray)
print(pytesseract.image_to_string(Image.fromarray(testarray))

这是测试文件的输出:

[[ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 ...
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]]

不过,这对 test1 来说效果很好。

[[[242 242 242 255]
  [242 242 242 255]
  [242 242 242 255]
  ...
  [242 242 242 255]
  [242 242 242 255]
  [242 242 242 255]]

 [[182 180 182 255]
  [182 180 182 255]
  [182 180 182 255]
  ...
  [182 180 182 255]
  [182 180 182 255]
  [182 180 182 255]]
g Request 4042337300021 submitted sucessfully

x
TYPE

我尝试使用 opencv 读取 tiff 文件,但格式不支持。

如何打印 tiff 或 tif 文件中的文本。

有什么建议吗?

问候, 任。

【问题讨论】:

    标签: python-3.x python-tesseract


    【解决方案1】:

    修改了我的整个代码并将tiff文件转换为jpeg文件,它能够读取文本。

    from PIL import Image, ImageSequence
    import pytesseract
    from pytesseract import image_to_string
    import numpy as np
    import cv2
    import os
    yourpath = r'C:\Python\'
    for root, dirs, files in os.walk(yourpath, topdown=False):
        for name in files:
            print(os.path.join(root, name))
            if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":
                if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):
                    print ("A jpeg file already exists for %s" % name)
                # If a jpeg is *NOT* present, create one from the tiff.
                else:
                    outfile = os.path.splitext(os.path.join(root, name))[0] + ".jpg"
                    try:
                        im = Image.open(os.path.join(root, name))
                        print ("Generating jpeg for %s" % name)
                        im.thumbnail(im.size)
                        im.save(outfile, "JPEG", quality=100)
                    except Exception as e:
                        print (e)
    
    test = Image.open(r'C:\Python\BG96254V1.jpeg')
    
    testarray = np.array(test)
    print(testarray)
    print(pytesseract.image_to_string(Image.fromarray(testarray)))
    

    这仅阅读第一页而不是页面列表。任何建议如何进行更改以阅读所有页面。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多