Python 将图像指定给numpy数组会改变图像的颜色
我加载一个图像并将其添加到一个空的numpy数组中:Python 将图像指定给numpy数组会改变图像的颜色,python,arrays,image,numpy,Python,Arrays,Image,Numpy,我加载一个图像并将其添加到一个空的numpy数组中: plt.imshow(im) plt.show() im = imread('/path_to_image',mode = 'RGB') array = np.zeros((1, 299, 299,3), dtype = np.int8) array[0][:,:,:3] = im print(array[0].shape) print(im.shape) plt.imshow(array[0]) plt.show() 我希望这两个图像在
plt.imshow(im)
plt.show()
im = imread('/path_to_image',mode = 'RGB')
array = np.zeros((1, 299, 299,3), dtype = np.int8)
array[0][:,:,:3] = im
print(array[0].shape)
print(im.shape)
plt.imshow(array[0])
plt.show()
我希望这两个图像在显示时看起来是一样的。当我将图像指定给具有以下属性的数组时,图像似乎发生了变化:
array = np.zeros((1, 299, 299,3), dtype = np.int8)
array[0][:,:,:3] = im
im
和array
应该具有相同的dtype
。您可以为零数组显式定义适当的dtype
(如@Divakar所建议),或者您也可以使用NumPy,如下所示:
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
im = io.imread('https://i.stack.imgur.com/crpfS.png')
array = np.zeros_like(im[np.newaxis, :])
array[0] = im
fig, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(im)
ax0.set_title('im')
ax1.imshow(array[0])
ax1.set_title('array[0]')
plt.show()
你不应该使用
np.uint8
:数组=np.zero((1299299,3),dtype=np.uint8)
?你是对的,使用uint有帮助!