需求:我爬取的图片是bytes格式,需要直接存到本地。
import urllib3
import os
#PIL图像处理标准库
from PIL import Image
from io import BytesIO
http = urllib3.PoolManager()
response = http.request('GET','f.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a29f863534d9725bc315d607c8e.jpg')
result = response.data
#将bytes结果转化为字节流
bytes_stream = BytesIO(result)
#读取到图片
roiimg = Image.open(bytes_stream)
# roiimg.show() #展示图片
#print(type(result))
#print(response.status)
imgByteArr = BytesIO() #初始化一个空字节流
roiimg.save(imgByteArr,format('PNG')) #把我们得图片以‘PNG’保存到空字节流
imgByteArr = imgByteArr.getvalue() #无视指针,获取全部内容,类型由io流变成bytes。
# dir_name = os.mkdir('baiduimg')
img_name = '1.jpg'
with open(os.path.join('baiduimg',img_name),'wb') as f:
f.write(imgByteArr)
转自:https://blog.csdn.net/wgPython/article/details/80740067
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。