图片和base64相互转化

# -*- coding: utf-8 -*-
import urllib2 as ulb
import base64


#用urllib2库链接网络图像
response=ulb.Request('http://s14.sinaimg.cn/mw690/5562b044tx6BkMgSR09ad&690')
fp=ulb.urlopen(response).read()        #打开网络图像文件句柄
#把图片转化为base64形式
a=base64.b64encode(fp)
print(a)

#把base64形式的图片转化为图片
imgData = base64.b64decode(a)
leniyimg = open('imgout.png','wb')
leniyimg.write(imgData)
leniyimg.close()

#转化本地图片为base64
f=open(r'c:\jb51.gif','rb') #二进制方式打开图文件
ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
f.close()

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/80755226
今日推荐