[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Converting hex data to image

dimplemathew.17 at wrote:

> Hi i have a similar challenge where i need to store the thumbnailPhoto
> attribute to my local db and display the image every-time user logs in.
> But this solution does work . data looks like this:

> import PIL
> from PIL import Image
> import io
> data = open("bytes.txt")
> my_data=(
> photo_inline = io.StringIO(my_data)
> photo =
> error:
> Traceback (most recent call last):
> File "", line 9, in <module>
> photo =
> File "", line 2657, in open
> % (filename if filename else fp))
> OSError: cannot identify image file <_io.StringIO object at 0x0367FD00>

Did you try

photo ="bytes.txt")


If the above code is illustrative, and you really need the bytes in memory 
remember to open the file in binary mode:

with open("bytes.txt", "rb") as instream:
    data =

To create the image later the file-like objects needs to produce bytes:

instream = io.BytesIO(data)  # not StringIO!
photo =