我正在尝试将图像转换为 numpy 数组并将其另存为文本/csv 文件。然后我尝试将 text/csv 文件的内容加载回图像中。
在整个过程中,维度、数据类型的像素值不能改变,以便准确地重建原始图像(不失真)。
到目前为止我所拥有的-
testim = cv2.imread('img.jpg') #reading the input image
numpyimg = np.array(testim) # Saving as a numpy array
# Checking the shape and image
cv2_imshow(numpyimg)
print(numpyimg.shape)
# Trying to save in csv
for i in numpyimg:
np.savetxt(fname="image_array.csv",delimiter=",",X=i)
# Check generated csv file after loading it
image_array = np.loadtxt(
fname="image_array.csv","
)
print("NumPy array: \n",image_array)
print("Shape: ",image_array.shape)
print("Data Type: ",image_array.dtype.name)
当我打印保存文件的内容时,我看到了什么 -
NumPy array that I could saved in a file:
[[ 70. 176. 153.]
[ 63. 170. 144.]
[ 57. 167. 139.]
...
[ 69. 118. 80.]
[ 67. 117. 77.]
[ 64. 114. 74.]]
Shape: (1040,3)
原图的数组虽然-
array([[[ 78,120,165],[ 63,105,150],[ 48,91,134],...,[ 22,80,51],[ 35,62],[ 49,76]],[[ 77,122,160],[ 62,109,147],[ 50,95,132],[ 24,84,54],[ 29,87,58],[ 38,96,67]],[[ 73,124,[ 66,143],116,137],[ 28,90,60],[ 26,86,56],[ 27,57]],[ 69,118,80],[ 67,117,77],[ 64,114,74]]],dtype=uint8)
shape: (780,1040,3)
这些看起来不太一样,我不明白出了什么问题。
有没有更简单、更准确的方法来解决这个问题?
我已经坚持了很长时间。任何帮助表示赞赏!