Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting hex data to image

3,168 views
Skip to first unread message

Shyam Parimal Katti

unread,
Nov 14, 2013, 10:32:52 AM11/14/13
to pytho...@python.org
I am implementing an authentication system(in Django) using LDAP as the backend(django-auth-ldap). 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.....'] ...... ]

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

Any help would be greatly appreciated.


Thanks,
Shyam

Tim Golden

unread,
Nov 14, 2013, 11:18:38 AM11/14/13
to pytho...@python.org
On 14/11/2013 15:32, Shyam Parimal Katti wrote:
> I am implementing an authentication system(in Django) using LDAP as the
> backend(django-auth-ldap). 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.....']*
> ...... ]
>
> How do I convert the hex data for an image to the actual image?

Well, the first few bytes suggest that it's a JPEG, so save the bytes as
"something.jpg" and there you have it: an actual image.

Alternatively, you could load it into PIL [1] / Pillow [2] and
manipulate it as you see fit...

TJG

[1] http://www.pythonware.com/products/pil/
[2] https://pypi.python.org/pypi/Pillow/


Ben Finney

unread,
Nov 14, 2013, 4:09:09 PM11/14/13
to pytho...@python.org
Shyam Parimal Katti <spk...@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

Ben Finney

unread,
Nov 14, 2013, 4:29:02 PM11/14/13
to pytho...@python.org
Ben Finney <ben+p...@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

Shyam Parimal Katti

unread,
Nov 14, 2013, 5:22:58 PM11/14/13
to Ben Finney, pytho...@python.org
Perfect. Thank you @Ben and @Tim


_o__)                                                                  |
Ben Finney

--
https://mail.python.org/mailman/listinfo/python-list

dimplem...@gmail.com

unread,
Mar 11, 2019, 6:38:45 AM3/11/19
to
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>

Peter Otten

unread,
Mar 11, 2019, 7:02:48 AM3/11/19
to
dimplem...@gmail.com 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:
>
\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff\xdb\x00C\x00

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

Did you try

photo = PIL.Image.open("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 = instream.read()

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

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


dimplem...@gmail.com

unread,
Mar 11, 2019, 11:40:20 PM3/11/19
to
Hey,
It shows the same error.
I am actually getting that image from ldap in bytes: '\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.....
I want to display this image on my template.

dimplem...@gmail.com

unread,
Mar 11, 2019, 11:42:54 PM3/11/19
to

Peter Otten

unread,
Mar 12, 2019, 4:39:06 AM3/12/19
to
Save the image to a file (in binary mode!) and then try to open it with an
image viewer. The data may be corrupted.

dimplem...@gmail.com

unread,
Mar 12, 2019, 5:14:54 AM3/12/19
to
When i tried doing that it says Invalid Image...

Peter Otten

unread,
Mar 12, 2019, 5:23:49 AM3/12/19
to
dimplem...@gmail.com wrote:

>> Save the image to a file (in binary mode!) and then try to open it with
>> an image viewer. The data may be corrupted.
>
> When i tried doing that it says Invalid Image...

So it looks like the problem occurs somewhere before you are decoding the
image with the PIL...


dimplem...@gmail.com

unread,
Mar 12, 2019, 5:31:45 AM3/12/19
to
This is what i am doing :
for associate in self.conn.response[:-1]:
attributes = associate['attributes']
obj = {
'img':attributes['thumbnailPhoto'],}
This img i am calling in my view and writing to the file...

Ali Sajadian

unread,
Feb 1, 2021, 2:19:44 AM2/1/21
to
dimplem...@gmail.com در تاریخ سه‌شنبه ۱۲ مارس ۲۰۱۹ ساعت ۱۳:۰۱:۴۵ (UTC+3:30) نوشت:
Hi could you solve this problem dimplem?
I have exactly the same issue when I read thumbnailPhoto from active directory

anas el amouri

unread,
Apr 27, 2022, 7:56:46 PM4/27/22
to
bonjour a tous ,la vidéo résout ce problème
https://www.youtube.com/watch?v=GkxsYFQj0QM&ab_channel=TheRocketProgrammer
0 new messages