我想将图像(图像:img(保存到数组中,以.csv格式保存数组,然后将.csv文件加载到其他数组中并显示图像。但最后的图像不是我以前保存的图像。相反,我只看到条纹和正方形。。。末尾的图片
代码:
import numpy as np
from PIL import Image
#saving the image to an array
a = np.array(Image.open("C:\Users\Patrick\Desktop\1.png").convert("L"))
#showing the image from array
image = Image.fromarray(a, "L")
image.show()
#saving the array to .csv file
array_save = np.savetxt("C:\Users\Patrick\Desktop\array_save.csv", a, delimiter=",")
array = np.loadtxt("C:\Users\Patrick\Desktop\array_save.csv", delimiter=",")
array = array.astype(int)
#showing the image from array put of the .csv file
image = Image.fromarray(array, "L")
image.show()
我真的不知道为什么我没有看到这张照片。数组应该是一样的,当我打印出来的时候,它们在我看来是一样的
创建的a
数组的类型为uint8
,但从CSV文件重新创建数组时,指定的是int
。将其更改为np.uint8
,它应该可以工作:
import numpy as np
from PIL import Image
#saving the image to an array
a = np.array(Image.open("1.png").convert("L"))
print(a.dtype) # show the array type
#showing the image from array
image = Image.fromarray(a, "L")
image.show()
#saving the array to .csv file
array_save = np.savetxt("array_save.csv", a, delimiter=",")
array = np.loadtxt("array_save.csv", delimiter=",")
array = array.astype(np.uint8)
#showing the image from array put of the .csv file
image = Image.fromarray(array, "L")
image.show()