5

I'm trying to get a jpeg, gif, etc. from http requests but I'm getting an error. Here is a part of the code:

def printimg(url):

    http = httppoolmgr()

    file = http.request('GET',url).read()
    r_data = binascii.unhexlify(file)
    stream = io.BytesIO(r_data)
    img = Image.open(stream)
    #img = Image.open(file)

    return img

the error code return is :

  File "C:\Python34\lib\site-packages\pillow-2.5.3-py3.4-win32.egg\PIL\Image.py"
, line 2256, in open
OSError: cannot identify image file <_io.BytesIO object at 0x02345ED8>

Has anyone already met this kind of problem?

Hugo
  • 27,885
  • 8
  • 82
  • 98
JroS
  • 65
  • 1
  • 2
  • 6
  • Not sure what your intent is here but you could use urllib to retrieve the image and then open as Image object and return. Check: https://docs.python.org/2/library/urllib.html#urllib.urlretrieve – user3885927 Oct 01 '14 at 21:59
  • Ok I think also but urllib3 bring new functionalities and I would like to use only this librairy if possible – JroS Oct 02 '14 at 16:11
  • 1
    Duplicate of http://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python? Image.open() expects a filename – Hugo Oct 04 '14 at 06:02

2 Answers2

-1

I replace the retrieve part of the image by the following code

response = requests.get(url)
img = Image.open(io.BytesIO(response.content))
img.save("picture/%s.png" % row)
self.foto = PhotoImage(file="picture/%s.png" % row)

Label(self.frame, image=self.foto, name=str(row)).grid(row=row, column=0, sticky=W)

Now there is no error at all about opening image or something else now I m getting an other problem with the following code:

def populate(self):

    http = httppoolmgr()
    array = xmltohash(getrack(http,'618cd2a4a2a1740a9f46e4f367ef88f3'))

    for row in range(len(array)):
        url = str((array[row]),"utf-8").split("$#$")[3]
        title = str((array[row]),"utf-8").split("$#$")[1]
        response = requests.get(url)
        img = Image.open(io.BytesIO(response.content))
        img.save("picture/%s.png" % row)
        self.foto = PhotoImage(file="picture/%s.png" % row)

        Label(self.frame, image=self.foto, name=str(row)).grid(row=row, column=0, sticky=W)
        t=str((array[row]),"utf-8").split("$#$")[1]
        Label(self.frame, text=t).grid(row=row, column=1, sticky=W)

Only the last image is displayed but all the text are displayed correctly in the frame. If someone can help please I'm a little bit stuck

Regards,

JRoS

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
JroS
  • 65
  • 1
  • 2
  • 6
  • Please don't ask a second question in your answer. You can post such things as new questions if need be, and link to the previous question if warranted for context. – CrazyChucky Nov 12 '21 at 10:08
-1

ok this the solution:

def populate(self):

    http = httppoolmgr()
    array = xmltohash(getrack(http,'618cd2a4a2a1740a9f46e4f367ef88f3'))

    for row in range(len(array)):
        url = str((array[row]),"utf-8").split("$#$")[3]
        title = str((array[row]),"utf-8").split("$#$")[1]
        response = requests.get(url)
        img = Image.open(io.BytesIO(response.content))
        img.save("picture/%s.png" % row)
        self.foto = PhotoImage(file="picture/%s.png" % row)

        pic=Label(self.frame, image=self.foto, name=str(row))
        pic.image = self.foto
        pic.grid(row=row, column=0, sticky=W)
        t=str((array[row]),"utf-8").split("$#$")[1]
        Label(self.frame, text=t).grid(row=row, column=1, sticky=W)

for my point of view there is no sense why the previous code was not working but I guess python or tkinter doesn't like to cast many options woinw no I know :D

JroS
  • 65
  • 1
  • 2
  • 6