如何像Matlab一样在Python中显示图片

How to show a picture in Python like Matlab

这是 zero 20 x 20 像素图像,该图像存储在大小为 400 的数组中:

X[0,:] = [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254
          254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 246
          232 206 206 246 254 255 255 255 255 255 255 255 255 255 255 255 255 255
          246 182 132 132 132 161 206 232 254 255 255 255 255 255 255 255 255 255
          255 254 206 132 132 132 132 132 132 182 246 255 255 255 255 255 255 255
          255 255 255 254 206 132 132 161 182 161 132 161 232 255 255 255 255 255
          255 255 255 255 255 254 182 132 161 232 246 182 132 161 232 255 255 255
          255 255 255 255 255 255 255 246 182 132 182 246 246 182 132 182 246 255
          255 255 255 255 255 255 255 255 255 246 182 132 182 254 232 161 132 206
          254 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 206 132
          161 232 254 255 255 255 255 255 255 255 255 255 255 246 182 132 161 206
          161 132 182 246 255 255 255 255 255 255 255 255 255 255 255 254 206 132
          132 132 132 132 206 254 255 255 255 255 255 255 255 255 255 255 255 255
          232 182 161 132 132 182 232 254 255 255 255 255 255 255 255 255 255 255
          255 255 254 246 232 206 206 232 254 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 254 254 254 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
          255 255 255 255]

在Matlab中,我可以使用这张图片displayData(X(0,:))命令看到这张图片。

此外,使用以下命令:

% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:100);
displayData(X(sel, :));

我可以显示不同的数字图片,例如:

我尝试使用以下代码在 Python 中执行相同的操作:

import matplotlib.pyplot as plt
plt.imshow(X[0, :])
plt.show()

但是,它抛出了这个错误:Output: TypeError: Invalid dimensions for image data

numpy 数组通常用于保存在 matplotlib 中绘图的数据,因此最容易将灰度图像字节读入 numpy 数组,告诉你的 numpy 数组形状

然后还有更多的情节设置

import numpy as np
import matplotlib.pyplot as plt

zstr = '255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 246 232 206 206 246 254 255 255 255 255 255 255 255 255 255 255 255 255 255 246 182 132 132 132 161 206 232 254 255 255 255 255 255 255 255 255 255 255 254 206 132 132 132 132 132 132 182 246 255 255 255 255 255 255 255 255 255 255 254 206 132 132 161 182 161 132 161 232 255 255 255 255 255 255 255 255 255 255 254 182 132 161 232 246 182 132 161 232 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 246 182 132 182 246 255 255 255 255 255 255 255 255 255 255 246 182 132 182 254 232 161 132 206 254 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 206 132 161 232 254 255 255 255 255 255 255 255 255 255 255 246 182 132 161 206 161 132 182 246 255 255 255 255 255 255 255 255 255 255 255 254 206 132 132 132 132 132 206 254 255 255 255 255 255 255 255 255 255 255 255 255 232 182 161 132 132 182 232 254 255 255 255 255 255 255 255 255 255 255 255 255 254 246 232 206 206 232 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255'    

z, z.shape = np.array([int(i) for i in zstr.split(' ')]), (20,20)

fig, ax = plt.subplots()
ax.imshow(z, cmap=plt.cm.gray, interpolation='nearest')
# interpolation=None uses a smoother default interpolatiopn
plt.show()

要在一个块中显示多个图像(如您问题的示例),我使用以下代码。 X4 是图像大小为 32x32 的数据,我显示 10X10 图像:

images_100=None
for i in range(10):
  row=None
  for j in range(10):
    if row is not None:
      row=np.concatenate((row,np.reshape(X4[i*j+i+j],(32,32)).T),axis=1)
    else:
      row=np.reshape(X4[i*j+i+j],(32,32)).T
  if images_100 is not None:
    images_100=np.concatenate((images_100,row))
  else:
    images_100=row
plt.imshow(images_100, cmap=plt.cm.gray)