解码base64编码的图像产生平面数组

时间:2019-01-08 08:10:46

标签: python numpy base64

我正在尝试通过将图像编码为base64将图像发送到Algorithmia服务器。但是,当我使用base64模块对其进行解码时,它会生成一维数组,不能用作图像。

img = cv2.imread("/home/abdullah/Desktop/Profile.png")
img = cv2.resize(img, (512, 512))
encoded_string = base64.b64encode(img)
img1 = base64.decodestring(encoded_string)
print(img1)

实际结果应类似于原始图像:

[[[ 87 129 255][ 88 128 255][ 90 130 255]...[ 54  80 174]

但是解码后的结果是:

[ 87 129 255 ...  51 100 156]

请帮助我!

1 个答案:

答案 0 :(得分:0)

尝试了很多解决方案之后,我终于找到了解决方案。

encoded_string = base64.b64encode(img)
img1 = base64.decodestring(encoded_string)
q = np.frombuffer(img1, dtype=np.uint8)  # Because image was in uint8
q = np.array(q)
q = q.reshape(512, 512,3)
plt.imshow(q)
plt.show()