Pillow OSError: cannot identify image file <_io.BytesIO object at 0x02345ED8>

I am trying to get jpeg, gif etc. from http requests, but I get an error. Here is the piece of 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

error code return:

  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 encountered such a problem?

+4
source share
2 answers

I replace the extracted part of the image with 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 when opening the image or something else, now I get another 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 text is displayed correctly in the frame. If anyone can help, I'm a little stuck.

Hi,

Jros

0
source

ok this 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)

, , , python tkinter , : D

0

All Articles