python如何将图片的像素矩阵绘制成图片(python,matplotlib):TypeError: Invalid shape (1, 28, 28) for image data

矩阵变成图片,这个问题使用(python , matplotlib ) 可以轻松实现。

import matplotlib.pyplot as plt

plt.imshow(x)

其中的x为:

array-like or PIL image
Supported array shapes are:
(M, N): an image with scalar data. The values are mapped to colors using normalization and a colormap. See parameters norm, cmap, vmin, vmax.

(M, N, 3): an image with RGB values (0-1 float or 0-255 int).

(M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.

The first two dimensions (M, N) define the rows and columns of the image.

Out-of-range RGB(A) values are clipped.

从上面我们可以看到,其只支持的矩阵形式为:

(M, N)
(M, N, 3)
(M, N, 4)

所以如果我们要把手写数字识别中的图片 (1,28,28)矩阵绘制成图片,需要把其压缩一个维度 (28,28)

否则报错:

TypeError: Invalid shape (1, 28, 28) for image data

python如何将图片的像素矩阵绘制成图片(python,matplotlib):TypeError: Invalid shape (1, 28, 28) for image data

正确的应该如下:

print(img.shape)
plt.imshow(img.squeeze(0))

python如何将图片的像素矩阵绘制成图片(python,matplotlib):TypeError: Invalid shape (1, 28, 28) for image data

Original: https://blog.csdn.net/qq_43391414/article/details/122115795
Author: 音程
Title: python如何将图片的像素矩阵绘制成图片(python,matplotlib):TypeError: Invalid shape (1, 28, 28) for image data

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/636942/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球