通过requests获取网络上图片的大小(¥52)

版权声明:转载请标明出处,谢谢了!https://blog.csdn.net/weixin_43721133 https://blog.csdn.net/weixin_43721133/article/details/89070223
from io import BytesIO,StringIO
import requests
from PIL import Image
img_url = "http://imglf1.ph.126.net/pWRxzh6FRrG2qVL3JBvrDg==/6630172763234505196.png"
response = requests.get(img_url)
f = BytesIO(response.content)
img = Image.open(f)
print(img.size)

结果:
(727, 403)

理解一下 BytesIO 和 StringIO
(很多时候,数据读写不一定是文件,也可以在内存中读写,
StringIO顾名思义就是在内存中读写str
BytesIO就是在内存中读写bytes类型的二进制数据)

例子中如果使用StringIO 即f = StringIO(response.text)会产生"cannot identify image file"的错误
当然上述例子也可以把图片存到本地之后再使用Image打开来获取图片大小

猜你喜欢

转载自blog.csdn.net/weixin_43721133/article/details/89070223