Pythn 图片转base64及base64转图片

2017-12-31  本文已影响0人  MingSha
import base64
#图片转Base64
image = open('Image 1.png', 'rb')
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
print(image_64_encode)

# Base64l转图片并保存
image_64_decode = base64.decodestring(image_64_encode)
image_result = open('656.gif', 'wb')
image_result.write(image_64_decode)

当你有多个base64字符串时,使用循环base64.decodestring(i)时,会出现编码错。
这时需要使用image_read.encode('ascii')进行处理一下,转为ascii后再执行

image_64_decode = base64.decodestring((image_read.encode('ascii')))
image_result.write(image_64_decode)

参考文献:https://stackoverflow.com/questions/8908287/why-do-i-need-b-to-encode-a-python-string-with-base64

上一篇下一篇

猜你喜欢

热点阅读