python 笔记1

在Python中将二进制流保存为文件的三种办法。第一种使用Image包,第二种使用分片写方法,第三种直接全部写。

coding=utf-8

import requests;
from PIL import Image;
from io import BytesIO;
path="http://hdn.xnimg.cn/photos/hdn421/20140815/1345/h_tiny_4pMx_27bd00048fe11986.jpg";
r=requests.get(path);

def test_io():
i=Image.open(BytesIO(r.content));
i.save("2.jpg");

def test_request():
with open("12.jpg","wb") as fd:
for chunk in r.iter_content(chunk_size=1024):
fd.write(chunk);

def test_request1():
with open("3.jpg","wb") as f:
f.write(r.content);

test_request1();

posted @ 2017-01-14 11:54  likeshu  阅读(154)  评论(0编辑  收藏  举报