fortran
fortran

Reputation: 76057

Load an image from an input stream in Python

I am using Python Imaging Library to do some processing, and I'd like to feed my script with data through a pipe rather than passing a file name as an argument.

I've seen that Image.open can accept any file-like object besides a string as parameter. I've tried passing sys.stdin directly, but it seems that it is having some problems with seek (logically).

So, how can I wrap the stream into a buffer that provides all the file operations? (or other any suitable solution to read the image directly from stdin).

Upvotes: 2

Views: 2477

Answers (1)

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 66970

Use StringIO.

import StringIO
import sys

filelike = StringIO.StringIO(sys.stdin.read())

# Now use `filelike` as a regular open file, e.g.:
filelike.seek(2)
print filelike.read()

Upvotes: 3

Related Questions