Converting hex data to image

dimplemathew.17 at gmail.com dimplemathew.17 at gmail.com
Mon Mar 11 06:38:32 EDT 2019


On Friday, November 15, 2013 at 3:52:58 AM UTC+5:30, Shyam Parimal Katti wrote:
> Perfect. Thank you @Ben and @Tim
> 
> 
> 
> 
> On Thu, Nov 14, 2013 at 4:29 PM, Ben Finney <ben+p... at benfinney.id.au> wrote:
> 
> 
> Ben Finney <ben+p... at benfinney.id.au> writes:
> 
> 
> 
> 
> > To turn a byte string into a file-like object for use with PIL, extract
> 
> > the byte string as ‘image_data’, use the standard library ‘io.StringIO’
> 
> > class <URL:http://docs.python.org/3/library/io.html#io.StringIO>, then
> 
> > create a new ‘PIL.Image’ object by reading from that pseudo-file::
> 
> 
> 
> My apologies, I showed the wrong usage. This should work::
> 
> 
> 
> 
>     import io
> 
> 
> 
>     import PIL
> 
> 
> 
>     photo_data = # … get the byte string from wherever it is …
> 
>     photo_infile = io.StringIO(photo_data)
> 
>     photo_image = PIL.Image.open(photo_infile)
> 
> 
> 
> That is, ‘PIL.Image.frombytes’ allows you to read the bytes from a byte
> 
> string, but requires you to also specify metadata about the image data
> 
> (format, pixel mode, size), whereas ‘PIL.Image.open’ reads the data from
> 
> a file-like object and parses all the metadata. So you usually want to
> 
> use the latter, as shown here.
> 
> 
> 
> --
> 
>  \         “People's Front To Reunite Gondwanaland: Stop the Laurasian |
> 
>   `\              Separatist Movement!” —wiredog, http://kuro5hin.org/ |
> 
> 
> 
> _o__)                                                                  |
> 
> Ben Finney
> 
> 
> 
> --
> 
> https://mail.python.org/mailman/listinfo/python-list

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: \xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff\xdb\x00C\x00
\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!222222222222222222222222222222222



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




More information about the Python-list mailing list