requests to download pictures
1. The first is to display the picture directly, using the following method
def download_img(imgurl):
try:
rsp = requests.get(imgurl)
if rsp.status_code == 200:
content = rsp.content
# 注意下面open里面的mode是"wb+", 因为content的类型是bytes
with open("./a.jpg", "wb+") as f:
f.write(content)
return str(content)
except Exception, err:
print 'load img err. err=', err
download_img("https://i.loli.net/2020/12/23/lkET5OzDHqsdh3Z.jpg")
2. If you want to convert the picture bytes format into base64 encoding, and then convert it into string type and store it in the database, you can use base64 for conversion
import base64
def downloadimg(imgurl):
try:
rsp = requests.get(imgurl)
if rsp.status_code == 200:
content = rsp.content
content = base64.b64encode(content) # 将图片转化成base64格式
return str(content)
except Exception, err:
print 'load img err. err=', err
downloadimg("https://i.loli.net/2020/12/23/lkET5OzDHqsdh3Z.jpg")
Reference article: https://blog.csdn.net/jy692405180/article/details/73272066
For local image processing, please refer to: https://blog.csdn.net/weixin_43507959/article/details/108501826