简体   繁体   English

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

[英]Decoding a base64 encoded image produces flat array

I am trying to send images to algorithmia server by encoding an image to base64. 我正在尝试通过将图像编码为base64将图像发送到Algorithmia服务器。 But when I use base64 module to decode it, it produces 1d array which can not be used as a image . 但是,当我使用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)

Actual result should be like the original image : 实际结果应类似于原始图像:

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

But after decoding result is : 但是解码后的结果是:

[ 87 129 255 ...  51 100 156]

Please help me ! 请帮我 !

After, trying lots of solution I finally got the solution. 之后,尝试了很多解决方案,我终于得到了解决方案。

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()

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

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