格式为base64到base64 obj的字符串

2024-03-29 13:45:15 发布

您现在位置:Python中文网/ 问答频道 /正文

从post请求中,我收到一个JSON格式的base64。问题是我不能将文件类型从string更改为base64,因为它已经格式化为base64。需要base64将其转换回图像。你知道吗

json_data = request.get_json(force=True)
img = json_data['img']
print(img)
with open("imageToSave.png", "wb") as fh:
   fh.write(base64.decodebytes(img))

Tags: 图像jsontrueimgdatagetstringrequest
1条回答
网友
1楼 · 发布于 2024-03-29 13:45:15

为了对其进行解码,将解码字符串的第三行添加到base64

json_data = request.get_json(force=True)
img = json_data['img']
imgdata = base64.b64decode(img)
filename = 'upload/newimg.jpg'  # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
        f.write(imgdata)

相关问题 更多 >