无法解码base64编码的图像

时间:2020-07-17 15:18:30

标签: python python-3.x python-requests base64

我是python的新手。我有一个任务来构建一个接受图像并返回图像的API端点。因此,我选择flask完成工作。

我遵循了这样的问题-Can a flask-based API return a file,要求API端点上传图像文件。

代码如下:

from flask import Flask, render_template , request , jsonify
from PIL import Image
import os , io , sys
import numpy as np 
import cv2
import base64

app = Flask(__name__)

start_point = (0, 0) 
end_point = (110, 110) 
color = (255, 0, 0)
thickness = 2

@app.route('/image' , methods=['POST'])
def mask_image():
    file = request.files['image'].read()
    npimg = np.fromstring(file, np.uint8)
    img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
    img = Image.fromarray(img.astype("uint8"))
    rawBytes = io.BytesIO()
    img.save(rawBytes, "png")
    rawBytes.seek(0)
    img_base64 = base64.b64encode(rawBytes.read())
    return jsonify({'status':str(img_base64)})


if __name__ == '__main__':
    app.run(debug = True)

然后我使用python requests file upload向API发送了一个请求。

但是我无法解码base64响应。我尝试过的代码

import requests
import base64

files = {'image': open('image.png','rb')}
r = requests.post("http://localhost:5000/image", files=files)
print(base64.decodestring(r.text))

但是它抛出了错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/py37/lib/python3.7/base64.py in _input_type_check(s)
    509     try:
--> 510         m = memoryview(s)
    511     except TypeError as err:

TypeError: memoryview: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-192-e8ba5f9daae3> in <module>
----> 1 base64.decodestring(r.text)

~/anaconda3/envs/py37/lib/python3.7/base64.py in decodestring(s)
    552                   "use decodebytes()",
    553                   DeprecationWarning, 2)
--> 554     return decodebytes(s)
    555
    556

~/anaconda3/envs/py37/lib/python3.7/base64.py in decodebytes(s)
    543 def decodebytes(s):
    544     """Decode a bytestring of base-64 data into a bytes object."""
--> 545     _input_type_check(s)
    546     return binascii.a2b_base64(s)
    547

~/anaconda3/envs/py37/lib/python3.7/base64.py in _input_type_check(s)
    511     except TypeError as err:
    512         msg = "expected bytes-like object, not %s" % s.__class__.__name__
--> 513         raise TypeError(msg) from err
    514     if m.format not in ('c', 'b', 'B'):
    515         msg = ("expected single byte elements, not %r from %s" %

TypeError: expected bytes-like object, not str

如何解码图像?

3 个答案:

答案 0 :(得分:1)

两件事,首先base64.decodestring需要字节,而不是字符串。您需要使用r.contentr.text.encode('utf8')来获取字节。同样,decodestring也已被弃用,因此您不应使用它。通常,除非有充分理由不这样做,否则应使用base64.b64decode来解码base64数据。

第二,/image端点返回一个包含base64图像的JSON对象。您的客户端将必须首先从JSON响应中提取图像数据,然后对其进行解码。 response对象包含一个对此有用的.json()方法:

import requests
import base64

files = {'image': open('image.png','rb')}
r = requests.post("http://localhost:5000/image", files=files)
print(base64.b64decode(r.json()['status']))

答案 1 :(得分:1)

标记的答案是我自己的。因此,我将提供答案。

其中一个用户@ToxicCode已经给出了99%的答案。但是这里有一个陷阱。脚本以字节字符串的形式发送响应,并包装成如下所示的字符串。

"b'iVBORw0KGgoAAAANSUhEUgAABXYAAAOOCAIAAAAru93tAAEAAElEQVR4nOz9eZRk+V3ffX5+98a+ZkbutbZ2gZAx6DFgsEGHHRtrsC0xFgezGGg3to8H2+NlQGjmSAI/D4wfGD/40AiZRTDgBwkMGI9AwjoCzGLZgLHYZEmou5bMjFwiMjPWu37nj4iqzOqur
...
..

字符串b中已经有字节。因此,如果您不关注此问题,那么如果您遵循@ToxicCode,将会遇到错误。因此,作为一种不好的方法,您可以使用ast.literal_eval转换为字符串,然后遵循@ToxicCode代码。

重要说明:请勿在生产服务器中使用ast.literal_eval()。相应地更改实现。

import ast
import requests
import base64

files = {'image': open('image.png','rb')}
r = requests.post("http://localhost:5000/image", files=files)
data = ast.literval_eval(r.json()['status'])
print(base64.b64decode(data))

答案 2 :(得分:0)

尝试使用 Date Shift heat_number 0 01-01-2020 A HA1 4 01-01-2020 A HA5 2 01-01-2020 A HA8 1 01-01-2020 A HA10 3 01-01-2020 A HA18A (它是字节)而不是r.content(它是字符串)。