【问题标题】:Python 2.7 version how do i check_outputPython 2.7 版本我如何检查输出
【发布时间】:2015-08-04 09:55:46
【问题描述】:
result = check_output(['ocrad', fn + '.ppm']).strip().replace(' ', '')

有问题..

File "coding.ch17.py", line 55, in <module>
ch17()
File "coding.ch17.py", line 48, in ch17
result = solve_ch17(fn)
File "coding.ch17.py", line 35, in solve_ch17
result = check_output(['ocrad', fn + '.ppm']).strip().replace(' ', '')
File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

你能帮帮我吗?

源代码

def solve_ch17(fn):
    black, white = (0, 0, 0), (255, 255, 255)
    bg, fg = white, black
    # open image
    img = Image.open(fn)
    mx, my = img.size
    pix = img.load()
    # remove background
    for x in range(mx):
        for y in range(my):
            if pix[x,y] == white:
                pix[x,y] = fg
            else:
                pix[x,y] = bg
    # split text
    lx, ly = 70, 23
    for c in range(6):
        for xi in range(9):
            for yi in range(12):
                x, y = lx + c*9 + xi, ly + yi
                if pix[x, y] == fg:
                    nx = 15 + c*20 + xi
                    pix[x, y], pix[nx, y] = bg, fg
    # scale up
    r = 3
    img = img.resize((int(round(mx * r, 2)), int(round(my * r, 2))), Image.BICUBIC) # NEAREST, BILINEAR, BICUBIC
    img.save(fn + '.ppm')
    result = check_output(['ocrad', fn, + '.ppm']).strip().replace(' ', '')
    return result

【问题讨论】:

  • 你能告诉我们什么是 check_Output 函数吗?
  • fn 的值是多少?您能否在result = 行之前显示更多代码

标签: python python-2.7 subprocess


【解决方案1】:

当您使用 explicit 参数作为列表调用 subprocess.check_output() 时,您应该指定您尝试在子进程中运行的应用程序的完整路径;除非您传递 shell=True,否则不会调用任何 shell。

这记录在subprocess.check_output 中,它是subprocess.Popen 的包装器

这从您从操作系统得到的错误中应该很明显:

OSError: [Errno 2] No such file or directory

这意味着ocrad 很可能在$PATH 中找不到,因此您必须指定其完整路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2018-08-27
    • 2021-04-26
    • 2013-12-09
    相关资源
    最近更新 更多