python 将图像编码/解码到Base64

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将图像编码/解码到Base64相关的知识,希望对你有一定的参考价值。

import base64

# encode
with open("image.jpg", "rb") as image_file:
    b64_img = base64.b64encode(image_file.read()) #bytes array

# decode

"""
# convert to bytearray if needed
b64_img = b64_img.encode()
b64_img = b64_img[2:]
"""

nparr = np.frombuffer(base64.decodebytes(bytes), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)

以上是关于python 将图像编码/解码到Base64的主要内容,如果未能解决你的问题,请参考以下文章