![Получение выходных данных только с первой страницы TIFF для извлечения hocr](https://rvso.com/image/1665767/%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%B2%D1%8B%D1%85%D0%BE%D0%B4%D0%BD%D1%8B%D1%85%20%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85%20%D1%82%D0%BE%D0%BB%D1%8C%D0%BA%D0%BE%20%D1%81%20%D0%BF%D0%B5%D1%80%D0%B2%D0%BE%D0%B9%20%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D1%8B%20TIFF%20%D0%B4%D0%BB%D1%8F%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20hocr.png)
Я ищу вашего руководства по коду, приведенному ниже. Я запускаю этот код для извлечения текста из многостраничного TIFF в формат hocr. Я получаю вывод с 1-й страницы TIFF, но остальные страницы пропущены.
# Python program to extract text from all the images in a folder
# storing the text in corresponding files in a different folder
# This is for hocr output, but there is error of getting only 1 page
from PIL import Image
import pytesseract as pt
import os
pt.pytesseract.tesseract_cmd = r'C:\Users\admin\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'
def main():
# path for the folder for getting the raw images
path ="D:\\input"
# path for the folder for getting the output
tempPath ="D:\\output"
# iterating the images inside the folder
for imageName in os.listdir(path):
inputPath = os.path.join(path, imageName)
img = Image.open(inputPath)
# applying ocr using pytesseract for python
text = pt.image_to_pdf_or_hocr(img, extension = 'hocr', config = (r'--oem 3 --psm 6'), lang ="eng")
fullTempPath = os.path.join(tempPath, 'time_'+imageName+".hocr")
print(text)
# saving the text for every image in a separate .hocr file
file1 = open(fullTempPath, "wb")
file1.write(text)
file1.close()
if __name__ == '__main__':
main()
решение1
РЕДАКТИРОВАТЬ:
Я проверил, может получиться filename
вместоPILLOW.Image
text = pt.image_to_pdf_or_hocr('D:\\input\\Best time to visit.tiff', extension='hocr', config=(r'--oem 3 --psm 6'), lang="eng")
Таким образом, он может работать tesseract
с оригиналом tiff
и преобразовывать все страницы в один текст hocr
.
ОРИГИНАЛЬНЫЙ ОТВЕТ:
Я взял ваш tiff
код и ссылку в моем комментарии и создал код, который сохраняет каждую страницу в отдельном файле. Он используется img.seek(page)
для выбора страницы. И он работает у меня с вашим файлом.
from PIL import Image
import os
folder = '/home/furas/Desktop'
filename = 'Best time to visit.tiff'
img = Image.open(os.path.join(folder, filename))
page = 0
while True:
try:
img.seek(page)
filename = f'page-{page+1}.png'
print('saving...', filename)
img.save(os.path.join(folder, filename))
page += 1
except EOFError:
# Not enough frames in img
break
Что-то похожее работает у меня в вашем коде
from PIL import Image
import pytesseract as pt
import os
pt.pytesseract.tesseract_cmd = r'C:\Users\admin\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'
# path for the folder for getting the raw images
path = "D:\\input"
# path for the folder for getting the output
tempPath = "D:\\output"
# iterating the images inside the folder
for imageName in os.listdir(path):
# only images
if imageName.lower().endswith(('.tiff', '.jpg', '.png')):
print(imageName)
inputPath = os.path.join(path, imageName)
img = Image.open(inputPath)
page = 0
while True:
try:
img.seek(page)
text = pt.image_to_pdf_or_hocr(img, extension='hocr', config=(r'--oem 3 --psm 6'), lang="eng")
print('page...', page)
page += 1
fullTempPath = os.path.join(tempPath, f"time_{imageName}_{page}.hocr")
#print(text)
# saving the text for every image in a separate .hocr file
file1 = open(fullTempPath, "wb")
file1.write(text)
file1.close()
except EOFError:
# Not enough frames in img
break
Он должен записывать каждую страницу отдельно, .hocr
потому что если вы попытаетесь записать много страниц .hocr
в один файл, то это создаст испорченные данные..hocr
Чтобы записать все страницы в один файл, вам придется использовать обычный текст.