Frage

I have a program that generates pictures and either saves them to a file or prints out the raw image data in standard output. I am using Python subprocess module to call the external program, catch its stdout data and create a Python image object from the data. I keep getting "Cannot identify image file" error, though. I am new to this part of Python. Can you please help if you know how to achieve this? Here is my code:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
raw = p.stdout.read()
buff = StringIO.StringIO()
buff.write(raw)
buff.seek(0)
im = Image.open(buff)
im.show()
War es hilfreich?

Lösung

The code looks fine to me. Try adding the lines...

assert len(raw) >= 4
assert raw.startswith('\x89PNG')

...directly after the line...

raw = p.stdout.read()

...just to ensure you're getting even vaguely valid data back.

Update

Try this...

from subprocess import check_output
from cStringIO import StringIO
from PIL import Image

raw = check_output(cmd)
buff = StringIO(raw)
im = Image.open(buff)
im.show()

Update #2

Problem is there's a bug in qrcode.exe whereby, when it's writing to stdout, it tries to convert UNIX line endings to DOS line endings.

Looks like the bug may have gotten fixed in a later version. Try: https://code.google.com/p/qrencode-win32/downloads/detail?name=qrcodegui_setup-3.3.1b.exe&can=1&q=qrencode-win32

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top