Home python将base64字符串转换为cv2中的numpy.ndarray
Post
Cancel

python将base64字符串转换为cv2中的numpy.ndarray

在开发过程中,android作为客户端,flask作为服务端。图片的通信通过base64字符串的形式进行,所以在服务器端通常需要进行解码。

解码过程中核心的包包括:cv2 io base64 imageio.

下面是使用方法:

1
2
3
4
5
6
7
8
9
10
from imageio import imread
import cv2
import io
import base64
bstring='R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7'
# reconstruct image as an numpy array
img = imread(io.BytesIO(base64.b64decode(bstring)))
cv2_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow("reconstructed.jpg", cv2_img)
cv2.waitKey(0)
This post is licensed under CC BY 4.0 by the author.

Android通过http中post方法上传数据

ubuntu中源码编译安装cuda版opencv