对图片进行base64编解码

import base64

imgPath = 'img.jpg'

def encode(img):
    with open(img, 'rb') as f:
        imageData = f.read()
        base64Data = base64.b64encode(image_data)
        print(base64Data) # b'***'
    return base64Data

def decode(code, name):
    with open(name, 'wb') as file:
        jiema = base64.b64decode(code)
        file.write(jiema)

ss = encode(imgPath)
print(ss)
with open("code.txt", "w") as f:
    f.write(str(ss))
decode(ss, 'decode.jpg')

你可能感兴趣的:(python,base64)