gpt4 book ai didi

python-3.x - Image_to_string 不使用 pytesseract 从 tiff 或 tif 文件中读取文本

转载 作者:行者123 更新时间:2023-12-03 22:19:19 24 4
gpt4 key购买 nike

我正在尝试从 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 文件中的文本。

有什么建议吗?

问候,任.

最佳答案

修改了我的整个代码并将 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)))

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

谢谢。

关于python-3.x - Image_to_string 不使用 pytesseract 从 tiff 或 tif 文件中读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52430601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com