user1038085 user1038085 - 1 year ago 178
Python Question

Python Imaging: load jpeg from memory

The problem is to load jpeg-encoded image from memory.

I receive a string from socket:

jpgdata =

and I know that this is jpeg-encoded image.

I need to decode it.
The most stupid solution is:

o = open("Output/1.jpg","wb")
dt ="Output/1.jpg")

The question is how to do the same thing in-memory?

Answer Source

PIL's object accepts any file-like object. That means you can wrap your Image data on a StringIO object, and pass it to Image.Open

from cStringIO import StringIO
file_jpgdata = StringIO(jpgdata)
dt =

Or, try just passing self.rfile as an argument to - it might work just as well. (That is for Python 2 - for Python 3 use from io import BytesIO)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download