1
votes

Running this command in Windows shell:

djpeg -pnm -gray text.jpg | gocr -

works as expected - the image is decoded by djpeg executable and passed to gocr which decodes the contents.

I would like to run similar to this task under Python - pipe PIL image to gocr without writing temp files. For example I can instruct PIL to write the image in PPM format (accepted by gocr executable) to stdin:

im.save(sys.stdin, 'PPM')

but anything I tried with pipes and subprocess module, gives me no joy.

Can someone suggest how to run this task through Python - pipe image from PIL to executable and get the text output from stdout?

1

1 Answers

1
votes

To write to gocr subprocess' stdin, you could use subprocess module:

from subprocess import Popen, PIPE

p = Popen(["gocr", "-"], stdin=PIPE)
im.save(p.stdin, 'PPM')
p.stdin.close()
p.wait()

If im.save() doesn't work with a pipe then convert the image to a bytestring first:

from subprocess import Popen, PIPE
from StringIO import StringIO

buf = StringIO()
im.save(buf, 'PPM')

p = Popen(["gocr", "-"], stdin=PIPE)
p.communicate(input=buf.getvalue())