Skip to content

Instantly share code, notes, and snippets.

@uchidama
Last active March 14, 2022 01:25
Show Gist options
  • Save uchidama/40f3076283e485e75bc904193ede6929 to your computer and use it in GitHub Desktop.
Save uchidama/40f3076283e485e75bc904193ede6929 to your computer and use it in GitHub Desktop.
Convert mnist binary on keras datasets to jpeg images.
import keras
from keras.datasets import mnist
import numpy as np
from PIL import Image, ImageOps
import os
def save_image(filename, data_array):
im = Image.fromarray(data_array.astype('uint8'))
im_invert = ImageOps.invert(im)
im_invert.save(filename)
# Load MNIST Data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
DIR_NAME = "JPEGImages"
if os.path.exists(DIR_NAME) == False:
os.mkdir(DIR_NAME)
# Save Images
i = 0
for li in [x_train, x_test]:
print("[---------------------------------------------------------------]")
for x in li:
filename = "{0}/{1:05d}.jpg".format(DIR_NAME,i)
print(filename)
save_image(filename, x)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment