MNIST是个比较好的数据源,大家学习图像算法的入门级必备数据,刚开始学习的时候,肯定想知道这到底是什么样的图片。但是数据存储的格式是28*28的图片转换一维784的数据。我们需要把数据做一次转换才能获取想要的图片。下面简单的给出一个到处数据的代码。
# import libraries
import tensorflow as tf
from tensorflow.examples.tutorials import mnist
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
from PIL import Image
import os
# hack for duplicated library on MacOS
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# save raw image
def save_raw():
# read data from mnist. if data not exist, will download automatically
mnist = input_data.read_data_sets("data/", one_hot=True)
# we save image raw data to data/raw/ folder
# if the folder not there, create it
save_dir = 'data/raw/'
if os.path.exists(save_dir) is False:
os.makedirs(save_dir)
# save 20 images in training dataset
for i in range(20):
# please attention,mnist.train.images[i, :] is ith image, sequence started from 0
image_array = mnist.train.images[i, :]
# the image in MNIST of TensorFlow, image is 784 length vector, we recover it to 28x28 image
image_array = image_array.reshape(28, 28)
# save image as mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpg
filename = save_dir + 'mnist_train_%d.jpg' % i
# save image_array as image
# use Image.fromarray to convert image,then call save function to save
# because Image.fromarray is not good to support float, we have to convert it to uint8 and then read as 'L'
Image.fromarray((image_array*255).astype('uint8'), mode='L').convert('RGB').save(filename)
save_raw()