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
importnumpyasnpimportosfromtensorflow.keras.preprocessing.imageimportimg_to_array,load_imgimportpandasaspdimage_dataset_dir="datasets/cifar-10/images"new_dataset_folder="datasets/cifar-10_new/"dataset={"image":[],"label": []}forlabelinos.listdir(image_dataset_dir): images_dir=image_dataset_dir+"/"+labelifnotos.path.isdir(images_dir):continuefor image_file in os.listdir(images_dir):ifnotimage_file.endswith(".jpg",".png",".tiff"):continueimg=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