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


Groups > comp.lang.python > #59478

Re: Converting hex data to image

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'url:pypi': 0.03; 'string': 0.09; 'bytes.': 0.09; 'pil': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type;': 0.09; 'python': 0.11; 'fetch': 0.16; 'file-like': 0.16; 'finney': 0.16; 'hex': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:[ 40': 0.16; 'subject:Converting': 0.16; 'subject:image': 0.16; 'library': 0.18; 'seems': 0.21; 'appears': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'skip:{ 20': 0.24; 'string,': 0.24; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; "skip:' 10": 0.31; 'extract': 0.31; 'writes:': 0.31; 'class': 0.32; 'url:python': 0.33; 'actual': 0.34; 'convert': 0.35; 'right?': 0.36; 'subject:data': 0.36; 'url:org': 0.36; 'turn': 0.37; 'server': 0.38; 'ben': 0.38; 'depends': 0.38; 'url:library': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'url:3': 0.61; 'skip:* 10': 0.61; 'further': 0.61; 'choose': 0.64; 'skip:\xe2 10': 0.65; '8bit%:40': 0.68; 'user,': 0.69; 'obvious': 0.74; 'photo': 0.78; 'url:reference': 0.84; '\xe2\x80\xa6': 0.84; 'url:image': 0.91; 'url:latest': 0.91; '\xe2\x80\x9cthe': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Converting hex data to image
Date Fri, 15 Nov 2013 08:09:09 +1100
References <CALVX-oYZzX8CEvP58=mfyk2yYXFRkk4e0t66bFn8na-h_BNpHw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host rasputin.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:u9ZAbgQAxTwEqRa+yXrelATn3YQ=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2624.1384463361.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384463361 news.xs4all.nl 16000 [2001:888:2000:d::a6]:46723
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59478

Show key headers only | View raw


Shyam Parimal Katti <spk265@nyu.edu> writes:

> When we fetch the data from the LDAP server for a particular valid
> user, the data associated with the user contains the thumbnail photo
> in hex representation. E.x.:
>
> [('CN=XX,OU=Users,OU=Accounts,DC=test,DC=com', {'msExchBlockedSendersHash':
> ['\xce'], 'mailNickname': ['test_user'], 'primaryGroupID': ['513'],
> 'logonCount': ['1021'], *thumbnailPhoto:
> ['\xef\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.....']*......
> ]

So the image data appears to be a byte string, is that right? You can
use the Pillow library <URL:https://pypi.python.org/pypi/Pillow/> to
process graphic images to and from bytes.

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::

    import io

    import PIL

    photo_data = # … get the byte string from wherever it is …
    photo_infile = io.StringIO(photo_data)
    photo_image = PIL.Image.frombytes(photo_infile)

> How do I convert the hex data for an image to the actual image?

It depends on what you think “the actual image” is, if not the bytes
themselves. There's no native Python “graphic image” data type; if the
bytes are not the representation you want, you need to choose some other
representation of that image.

Using a ‘PIL.Image’ object as the representation, you can perform all
kinds of further manipulations of a graphic image
<URL:http://pillow.readthedocs.org/en/latest/reference/Image.html>.

-- 
 \      “It seems intuitively obvious to me, which means that it might |
  `\                                           be wrong.” —Chris Torek |
_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:09 +1100

csiph-web