python Numpy.ndsarray.tostring()

在项目中,需要把图像输入subprocessing.Popen(),但是管道子进程无法直接输入图像矩阵,因此,需要把矩阵转化为字符串。使得np.ndarray转化为string(or bytes)  ——>  利用tostring。但是这个方法会丢失原始数据中的类型信息(type)和维度信息(shape),这意味着你要将这两个信息一起穿到管道。再用 np.fromstring()转化为原来的矩阵

#coding=utf-8
import numpy as np

a=np.arange(15).reshape(3,5)
print a
b=a.tostring()
#print b
c=np.fromstring(b,np.int32).reshape(3,5)
print c

结果:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]

 [10 11 12 13 14]]

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]




猜你喜欢

转载自blog.csdn.net/nanxiaoting/article/details/80721051