OSError:无法识别图像文件< _io.BytesIO对象0x02F41960>同时尝试重用下载的标题

时间:2017-07-31 22:48:51

标签: python image python-3.x python-requests pillow

我的项目在Python 3.6.2中。我只是通过只读取标题(在线文件的第一个~100个字节)来尝试识别图像是否值得下载(如果它们具有一定的宽高比),到目前为止只是使用imghdr和Pillow进行测试。 / p>

Image.open最后以:

失败
  

文件" C:\ Program Files   (x86)\ Python36-32 \ lib \ site-packages \ PIL \ Image.py",第2349行,处于打开状态       %(文件名,如果是文件名,则为fp))

     

OSError:无法识别图像文件< _io.BytesIO对象位于0x02F41960>

我发现Release notes 2.8.0 for Pillow似乎表明我可以使用Image.open(requests.raw)。我猜想在确保用seek(0)重置之后我应该能够重用已经下载的头文件。

此错误的其他答案似乎涉及将图像缓冲区保存到实际文件,我试图避免(只是重复使用来自response.raw的下载字节进行所有测试/检查,而不是向任何服务器发出多个下载请求。)

我哪里出错了?

以下是我的示例代码:

import requests
from PIL import Image
import imghdr
import io

if __name__ == '__main__':
    url = "https://ichef-1.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg"
    try:
        response = requests.get(url, stream=True)
        if response.status_code == 200:
            response.raw.decode_content = True

            # Grab first 100 bytes as potential image header
            header = response.raw.read(100)
            ext = imghdr.what(None, h=header)
            print("Found: " + ext)
            if ext != None:     # Proceed to other tests if we received an image at all
                header = io.BytesIO(header)
                header.seek(0)
                im = Image.open(header)
                im.verify()

                # other image-related tasks here
        else:
            print("Received error " + str(response.status.code))
    except requests.ConnectionError as e:
        print(e)

1 个答案:

答案 0 :(得分:0)

在调用Image.open()之前,您必须获取其余的图像数据。

这就是我的意思:

import requests
from PIL import Image
import imghdr
import io

if __name__ == '__main__':
    url = "https://ichef-1.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg"
    try:
        response = requests.get(url, stream=True)
        if response.status_code == 200:
            response.raw.decode_content = True

            # Grab first 100 bytes as potential image header
            header = response.raw.read(100)
            ext = imghdr.what(None, h=header)
            print("Found: " + ext)
            if ext != None:     # Proceed to other tests if we received an image at all
                data = header + response.raw.read()  # GET THE REST OF THE FILE
                data = io.BytesIO(data)
                im = Image.open(data)
                im.verify()

                # other image-related tasks here
        else:
            print("Received error " + str(response.status.code))
    except requests.ConnectionError as e:
        print(e)