Creating images from Keras datasets

The Keras machine learning framework has a number of built-in datasets including MNIST digits, MNIST Fashion, CIFAR-10 and CIFAR-100. These datasets can be fetched using the .load_data() method like this:

  1. (x_train, y_train), (x_test, y_test) = cifar10.load_data()

This method returns tuples of numpy arrays as required for working with Keras. However, it can useful at times to convert the datasets into image files, in PNG, JPEG or Bitmap format. The script which can be found on my Github page allows users to select one of those datasets, then convert any number of the numpy arrays to images.

Command line arguments

The script has several command line arguments:

Argument Type Default Description
--dataset or -d string mnist The dataset to be used: cifar10,cifar100,mnist,fashion_mnist
--subset or -s string train The subset to be used: train or test
--image_dir or -dir string image_dir Path to folder for saving the images and images list file
--image_list or -l string image_list.txt Name of images list file
--image_format or -f string png Image file format - valid choices are png, jpg or bmp
--max_images or -m integer 1000 Number of images to generate


  • os
  • argparse
  • openCV
  • TensorFlow

Opening the generated images with OpenCV imread

Care should be taken when opening the images using OpenCV's imread function - especially for the MNIST digits and Fashion datasets. Opening those images with the default version of imread like this:

  1. image = cv2.imread('./mnist_jpg/image_0.jpg')

..will result in array that has a shape of (28, 28, 3), whereas using the IMREAD_GRAYSCALE flag will result in an array that has shape (28, 28) which can then be reshaped to [28,28,1] for use in TensorFlow:

  1. image = cv2.imread('./mnist_jpg/image_0.jpg', cv2.IMREAD_GRAYSCALE)
  2. image = np.reshape(image, [28, 28, 1])