What size to specify to `PIL.Image.frombytes`
The size
argument must match the image dimensions, which are not encoded in a raw pixel buffer (e.g. a buffer of length n can represent any grid of k×m pixels for k, m > 0, k×m = n). You have to know this size in advance.
Some example code to demonstrate both tobytes
and frombytes
:
>>> img = PIL.Image.open("some_image.png")
>>> img.size
(482, 295)
>>> raw = img.tobytes()
>>> img2 = PIL.Image.frombytes(img.mode, img.size, raw)
Since you clarified, that you don't want to read raw pixel data, but rather in-memory image file, the solution is clear: don't use frombytes
- it is meant for raw pixel data. Use just open from StringIO
:
image = Image.open(StringIO.StringIO(image_data))