The problem is to load jpeg-encoded image from memory.
I receive a string from socket:
jpgdata = self.rfile.read(sz)
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") o.write(jpgdata) o.close() dt = Image.open("Output/1.jpg")
The question is how to do the same thing in-memory?
Images are typically in PNG or JPEG format and can be loaded directly using the open() function on Image class. This returns an Image object that contains the pixel data for the image as well as details about the image.
image. load() function call will return a Surface object that has the image drawn on it. This Surface object will be a separate Surface object from the display Surface object, so we must blit (that is, copy) the image's Surface object to the display Surface object.
What is PIL/Pillow? PIL (Python Imaging Library) adds many image processing features to Python. Pillow is a fork of PIL that adds some user-friendly features.
PIL's Image.open 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 io import BytesIO file_jpgdata = BytesIO(jpgdata) dt = Image.open(file_jpgdata)
Or, try just passing self.rfile
as an argument to Image.open - it might work just as well. (That is for Python 3 - for Python 2 use from cStringIO import StringIO as BytesIO
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With