python读入mnist 存图片
时间: 2023-06-28 22:14:45 浏览: 132
如果你已经下载了MNIST数据集,可以用Python中的`numpy`和`matplotlib`库来读取和展示MNIST图片。以下是一个简单的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 读入MNIST数据集
with open('train-images-idx3-ubyte', 'rb') as f:
magic, num, rows, cols = np.fromfile(f, dtype=np.dtype('>i4'), count=4)
images = np.fromfile(f, dtype=np.uint8).reshape(num, rows, cols)
# 展示第一张图片
plt.imshow(images[0], cmap='gray')
plt.show()
# 存储第一张图片
plt.imsave('mnist_0.png', images[0], cmap='gray')
```
在这个示例中,我们首先使用`numpy.fromfile()`函数从文件中读取MNIST数据集,并使用`numpy.reshape()`函数将读入的数据转换为图像的形状。然后,我们使用`matplotlib.pyplot.imshow()`函数展示第一张图片,并使用`matplotlib.pyplot.imsave()`函数将第一张图片存储为PNG格式的文件。
请注意,这只是一个简单的示例代码,实际上需要更多的代码来读取MNIST数据集中的标签等其他信息。
阅读全文
相关推荐


















