import base64
#圖片轉(zhuǎn)Base64
image = open('Image 1.png', 'rb')
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
print(image_64_encode)
# Base64l轉(zhuǎn)圖片并保存
image_64_decode = base64.decodestring(image_64_encode)
image_result = open('656.gif', 'wb')
image_result.write(image_64_decode)
當(dāng)你有多個(gè)base64字符串時(shí)涡尘,使用循環(huán)base64.decodestring(i)
時(shí)笤妙,會(huì)出現(xiàn)編碼錯(cuò)互拾。
這時(shí)需要使用image_read.encode('ascii')
進(jìn)行處理一下,轉(zhuǎn)為ascii后再執(zhí)行
image_64_decode = base64.decodestring((image_read.encode('ascii')))
image_result.write(image_64_decode)
參考文獻(xiàn):https://stackoverflow.com/questions/8908287/why-do-i-need-b-to-encode-a-python-string-with-base64