Create Numpy dataset from images

If you have images and you want convert it to numpy, follow the below steps

Firstly, you need to install Tensorflow

$ pip install tensorflow

After that, run the below code which takes the image directory as input i.e in (.jpg, .png, .tiff) format and convert it to the NumPy format i.e (.npz)

conver_img_to_numpy.py
import numpy as np
import os
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import pandas as pd


image_dataset_dir = "datasets/cifar-10/images"
new_dataset_folder = "datasets/cifar-10_new/"


dataset = {
    "image" :[],
    "label" : []
}
for label in os.listdir(image_dataset_dir):
     images_dir= image_dataset_dir + "/" + label
     if not os.path.isdir(images_dir):
        continue
     for image_file in os.listdir(images_dir):
        if not image_file.endswith(".jpg", ".png",".tiff"):
            continue 
        img = load_img(os.path.join(image_dataset_dir, label, image_file))
        x = img_to_array(img)                  
        

        rel_path = label + "/" + os.path.splitext(image_file)[0] + '.npz'
        os.makedirs(new_dataset_folder + "/" + label, exist_ok=True)
        npz_file = os.path.join(new_dataset_folder, rel_path)
        np.savez(npz_file, x)
        dataset["image"].append(rel_path)
        dataset["label"].append(label)

                         
df = pd.DataFrame(dataset)
df.to_csv(os.path.join(new_dataset_folder, "train.csv"), index=False)

print('Dataset converted to npz and saved here at %s '%new_dataset_folder)

df.head()

Congratulation!

You have successfully converted your image dataset into NumPy format

Last updated