Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python tesseract结果会在句子之间产生多余的行距_Python_Tesseract - Fatal编程技术网

python tesseract结果会在句子之间产生多余的行距

python tesseract结果会在句子之间产生多余的行距,python,tesseract,Python,Tesseract,我正在用tesseract执行一些OCR操作。我已经为此编写了一个简单的python包装器。问题是我在结束文本文件中的句子之间得到了不需要的行距,我需要通过编程来删除这些行距。例如: 1 tbsp peanut or corn oil, plus a little extra for Cooking the scallops 2 tbsp bottled mild or medium Thai green curry paste 2 tbsp water 2 tsp light soy sa

我正在用tesseract执行一些OCR操作。我已经为此编写了一个简单的python包装器。问题是我在结束文本文件中的句子之间得到了不需要的行距,我需要通过编程来删除这些行距。例如:

1 tbsp peanut or corn oil, plus a little
extra for Cooking the scallops

2 tbsp bottled mild or medium Thai
green curry paste
2 tbsp water

2 tsp light soy sauce
请注意一些线间距--我需要删除这些线间距。如果您遇到类似问题,请分享一些提示。多谢各位

这是包装纸:

from PIL import Image
import subprocess
import os
from wand.image import Image
import markdown2
from textblob import TextBlob

import util
import errors

tesseract_exe = "tesseract" # Name of executable to be called at command line
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True # Temporary files cleaned up after OCR operation
pagesegmode = "-psm 0"


def call_tesseract(input_file, output_file):
    args = [tesseract_exe, input_file, output_file, pagesegmode]
    proc = subprocess.Popen(args)
    retcode = proc.wait()
    if retcode !=0:
        errors.check_for_errors()


def retrieve_text(scratch_text_name_root):
    inf = file(scratch_text_name_root + '.txt')
    text = inf.read()
    inf.close()
    return text

def write_to_file(filename, string):
    File = open(filename, 'w')
    File.write(string)
    File.close()


def image_to_string(filename):
    try:
        call_tesseract(filename, scratch_text_name_root)
        text = retrieve_text(scratch_text_name_root)
    finally:
        try:
            os.remove(scratch_text_name_root)
        except OSError:
            pass

        return text    

filename = "book/0001.bin.png"
text = image_to_string(filename)
print "writing to file"
write_to_file("0002.bin.txt", text)

我不知道为什么tesseract会给你这些空行,但也许有一个简单的解决方法可以帮助你:

只需删除这些空行。 有很多方法可以做到这一点,例如,请看这里:

或在此:

这些解决方案都假设您逐行读取文件


我喜欢这一点,因为您可以在完成的字符串中直接使用它,并且它可以处理操作系统在行尾上的差异(\n\n\r\r\n)。

谢谢您的链接。非常好的建议。我投票支持你。再次感谢您抽出时间。