最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Python的Pillow打开图片出错:OSError cannot identify image file _io.BytesIO object at

Python crifan 25浏览 0评论
折腾:
【未解决】Python中判断本地和网络图片的分辨率等基本信息
期间,此处先用:
import requests

def getImgRawData(imgUrl):
    respImgStream = requests.get(imgUrl, stream=True)
    imgRawData = respImgStream.raw
    print("imgRawData=%s" % imgRawData)
    return imgRawData
调试出:
返回的image的stream中text首字母是Gif
-》看起来是gif图片?
结果还是出错:
发生异常: OSError
cannot identify image file <_io.BytesIO object at 0x1125d3888>
2.8.0 — Pillow (PIL Fork) 3.0.0 documentation
加上:
respImgStream = requests.get(imgUrl, stream=True)
respImgStream.raw.decode_content = True
imgRawData = respImgStream.raw
结果:
依旧出错:
发生异常: OSError
cannot identify image file <_io.BytesIO object at 0x109724ba0>
难道是:
不能在函数中返回?
改为直接代码使用试试:
print("curImgUrl=%s" % curImgUrl)
# imgRawData = getImgRawData(curImgUrl)

respImgStream = requests.get(curImgUrl, stream=True)
respImgStream.raw.decode_content = True
imgRawData = respImgStream.raw
print("imgRawData=%s" % imgRawData)

img = Image.open(imgRawData)
结果:
错误依旧。
Image Module — Pillow (PIL Fork) 5.3.0 documentation
PIL.Image.open(fp, mode='r')[source]
Opens and identifies the given image file.
This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load()method). See new(). See File Handling in Pillow.
Parameters:
* fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
* mode – The mode. If given, this argument must be “r”.
很明显:
此处只支持File,而不是raw data
Add support for HTTP response objects to Image.open() by mfitzp · Pull Request #1151 · python-pillow/Pillow
Pillow/CHANGES.rst at master · python-pillow/Pillow
Python – how to read an image from a URL? – Stack Overflow
貌似只能下载保存到临时文件,再打开了?
scikit-image: Image processing in Python — scikit-image
参考:
Python3:urllib中urlopen()函数新特点 – Penguin
得知了:
之前Python2中的urllib.urlopen返回但是类似file的addinfourl对象
“addinfourl对象实际上是一个类似于文件的对象,大概包括read()、readline()、readlines()、fileno()、close()、info()、getcode()和geturl()函数,”
-》但是我此处用的是Python3
-》去找找对应的是啥
Python3学习笔记(urllib模块的使用) – Data&Truth – 博客园
“直接用urllib.request模块的urlopen()
urlopen返回对象提供方法:
–         read() , readline() ,readlines() , fileno() , close() :对HTTPResponse类型数据进行操作”
感觉Python3中好像是:
urllib.request.urlopen
去试试,果然可以返回类似file的对象了。
【总结】
此处Python3中,用:
import urllib

def getImgFile(imgUrl):
    imgFd = urllib.request.urlopen(imgUrl)
    # imgFileData = imgFd.read()
    # imgString = cStringIO.StringIO(imgFileData)
    # print("imgFile=%s" % imgFile)
    return imgFd

imgFile = getImgFile(curImgUrl)
img = Image.open(imgFile)
即可正常加载图片了:
然后可以通过size看到图片尺寸信息了:

转载请注明:在路上 » 【已解决】Python的Pillow打开图片出错:OSError cannot identify image file _io.BytesIO object at

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.082 seconds, using 9.33MB memory