Ich bitte um Ihre Anleitung zum unten angegebenen Code. Ich führe diesen Code aus, um Text aus mehrseitigem TIFF in das Hocr-Format zu extrahieren. Ich erhalte eine Ausgabe von der ersten Seite des TIFF, aber die restlichen Seiten werden ausgelassen.
# 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()
Antwort1
BEARBEITEN:
Ich habe es überprüft, filename
anstattPILLOW.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")
Es kann also tesseract
mit dem Original ausgeführt werden tiff
und wandelt alle Seiten in einen Text um hocr
.
URSPRÜNGLICHE ANTWORT:
Ich habe Ihren tiff
Code aus dem Link in meinem Kommentar genommen und Code erstellt, der jede Seite in einer separaten Datei speichert. Er dient img.seek(page)
zum Auswählen von Seiten. Und bei mir funktioniert es mit Ihrer Datei.
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
Bei mir funktioniert sowas ähnliches auch in deinem Code
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
Es muss jede Seite einzeln geschrieben werden .hocr
, denn wenn Sie versuchen, viele in eine Datei zu schreiben .hocr
, dann entstehen beschädigte.hocr
Um alle Seiten in eine Datei zu schreiben, müssten Sie einfachen Text verwenden.