Re: Converting hex data to image
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 wrote: > > > Ben Finney 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 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!2 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 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> -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting hex data to image
On Monday, March 11, 2019 at 4:32:48 PM UTC+5:30, Peter Otten wrote: > dimplemathew...@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 > > 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) 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting hex data to image
On Thursday, November 14, 2013 at 9:02:52 PM UTC+5:30, 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? > > Any help would be greatly appreciated. > > > Thanks, > Shyam 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting hex data to image
On Tuesday, March 12, 2019 at 2:09:06 PM UTC+5:30, Peter Otten wrote: > dimplemathew...@gmail.com wrote: > > > On Monday, March 11, 2019 at 4:32:48 PM UTC+5:30, Peter Otten wrote: > >> dimplemathew...@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 > >> > 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) > > > > 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. > > 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... -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting hex data to image
On Tuesday, March 12, 2019 at 2:53:49 PM UTC+5:30, Peter Otten wrote: > dimplemathew...@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... 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... -- https://mail.python.org/mailman/listinfo/python-list