如何将一个emnist的数据提取为60*60像素的图片
时间: 2024-02-18 07:03:04 浏览: 151
首先,需要使用emnist数据集的库来加载数据。然后,将每个图像重新调整大小为60x60像素,然后保存为图像文件。
以下是一个示例代码,可以将emnist数据集中的数据提取为60x60像素的图像:
```python
import numpy as np
import emnist
from PIL import Image
# Load EMNIST data
train_images, train_labels = emnist.extract_training_samples('digits')
test_images, test_labels = emnist.extract_test_samples('digits')
# Resize images to 60x60 pixels
new_size = (60, 60)
train_images_resized = np.array([np.array(Image.fromarray(img).resize(new_size)) for img in train_images])
test_images_resized = np.array([np.array(Image.fromarray(img).resize(new_size)) for img in test_images])
# Save resized images as image files
for i in range(len(train_images_resized)):
im = Image.fromarray(train_images_resized[i])
im.save('train_{}.png'.format(i))
for i in range(len(test_images_resized)):
im = Image.fromarray(test_images_resized[i])
im.save('test_{}.png'.format(i))
```
此代码将EMNIST数据集中的数字提取为60x60像素的图像,并将其保存为PNG文件。
阅读全文