在做python3网络爬虫爬取图片的时候,可能会涉及到判断图片大小。比如我们只采集某种大小的图片。下面是判断网络图片大小的代码:

import requests
from io import BytesIO
from PIL import Image
 
 
url = 'https://sc01.alicdn.com/kf/HLB1yTzIRQvoK1RjSZFwq6AiCFXa9/Wholesale-high-quality-New-design-fashion-basketball.jpg_350x350.jpg'
response = requests.get(url)

print(type(response))


tmpIm = BytesIO(response.content)

fsize=len(tmpIm.getvalue())#获取图片的字节数

print('文件大小:',fsize/1024/1024,'M') 


im = Image.open(tmpIm)

 
# 长宽储存在im当中的size列表当中
w = im.size[0]
h = im.size[1]
 
print("宽度:%s" % (w))
print("高度:%s" % (h))

发表评论