简体   繁体   English

使用 Python 解码 JSON 中的 base64 图像

[英]Decode base64 image in JSON with Python

I receive from front-end an image in base64 into JSON file, and need take image and decode with OpenCV, the JSON there is: I receive from front-end an image in base64 into JSON file, and need take image and decode with OpenCV, the JSON there is:

{
     'photo': "b'/9j/4AAQSkZJR...(continue)"
}

and code is和代码是

obj = json.loads(json.dumps(event))
obj = obj['foto']
obj = bytes(obj,'utf-8')

obj_d = base64.decodebytes(obj)
print(type(obj_d))

img_buffer = np.frombuffer(obj_d, dtype=np.uint8)
print(img_buffer)
img = cv2.imdecode(img_buffer, flags=cv2.IMREAD_COLOR)
print(img.shape)

And received error is, AttributeError: 'NoneType' object has no attribute 'shape'.收到的错误是,AttributeError: 'NoneType' object has no attribute 'shape'。

When I code and decode in base64 the image in the Python, I not have problem.当我在 base64 中编码和解码 Python 中的图像时,我没有问题。

In the JSON I wrote previous version(not error in here), the error is in b '...', I deleted this in front-end or with other method and works在JSON中我写了以前的版本(这里没有错误),错误在b '...'中,我在前端或其他方法删除了它并且有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM