![첫 번째 페이지 TIFF에서만 hocr 추출로 출력 가져오기](https://rvso.com/image/1665767/%EC%B2%AB%20%EB%B2%88%EC%A7%B8%20%ED%8E%98%EC%9D%B4%EC%A7%80%20TIFF%EC%97%90%EC%84%9C%EB%A7%8C%20hocr%20%EC%B6%94%EC%B6%9C%EB%A1%9C%20%EC%B6%9C%EB%A0%A5%20%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0.png)
아래 코드에서 귀하의 지침을 구합니다. 다중 페이지 TIFF에서 hocr 형식으로 텍스트를 추출하기 위해 이 코드를 실행하고 있습니다. 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
모든 페이지를 하나의 파일에 쓰려면 일반 텍스트를 사용해야 합니다.