numpy数组的可变大小
Variable size of numpy array
我正在使用 skimage.io.imread
加载图像并将图像保存为 numpy 数组。以下为推论:
原图:(256x512x3)
以下是我执行的代码:
img = io.imread(img_file) # 48.1 kB
i1, i2 = img[:, :256], img[:, 256:]
np.save('i1', i1) # 196.7 kB
np.save('i2', i2) # 196.7 kB
final_image = np.empty([1, 2, 256, 256, 3])
final_image[0, 0], final_image[0, 1] = i1, i2
np.save('final', final_image) # 3.1 MB
谁能解释一下为什么图像的大小差异如此之大?
编辑:i1
、i2
、final_image
的 dtype 是 np.float64
numpy.empty
将默认为您系统上的任何 np.float_
,但是,您的图像应该已读入为 np.uint8
,因此请提供相应的 dtype
empty
:
final_image = np.empty([1, 2, 256, 256, 3], dtype=np.uint8)
我正在使用 skimage.io.imread
加载图像并将图像保存为 numpy 数组。以下为推论:
原图:(256x512x3)
以下是我执行的代码:
img = io.imread(img_file) # 48.1 kB
i1, i2 = img[:, :256], img[:, 256:]
np.save('i1', i1) # 196.7 kB
np.save('i2', i2) # 196.7 kB
final_image = np.empty([1, 2, 256, 256, 3])
final_image[0, 0], final_image[0, 1] = i1, i2
np.save('final', final_image) # 3.1 MB
谁能解释一下为什么图像的大小差异如此之大?
编辑:i1
、i2
、final_image
的 dtype 是 np.float64
numpy.empty
将默认为您系统上的任何 np.float_
,但是,您的图像应该已读入为 np.uint8
,因此请提供相应的 dtype
empty
:
final_image = np.empty([1, 2, 256, 256, 3], dtype=np.uint8)