Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59481

Re: Converting hex data to image

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Converting hex data to image
Date 2013-11-15 08:29 +1100
References <CALVX-oYZzX8CEvP58=mfyk2yYXFRkk4e0t66bFn8na-h_BNpHw@mail.gmail.com> <7w61ruwv0q.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.2626.1384464557.18130.python-list@python.org> (permalink)

Show all headers | View raw


Ben Finney <ben+python@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Converting hex data to image Ben Finney <ben+python@benfinney.id.au> - 2013-11-15 08:29 +1100

csiph-web