Skip to content

Instantly share code, notes, and snippets.

@Shivam60
Last active March 18, 2019 14:43
Show Gist options
  • Save Shivam60/ea0032bb20f901a52737466f8df18045 to your computer and use it in GitHub Desktop.
Save Shivam60/ea0032bb20f901a52737466f8df18045 to your computer and use it in GitHub Desktop.
ConvertImagesToNumpyArray
"""
Convert all the images in a given folder in a numpy folder.
Additionally a stats dict is given out where keys are the the image shape and the values are the number of images belonging to that image
"""
import numpy as np
import os
from PIL import Image
def ConvertImagesToNumpyArray(Path):
folder=[]
stats={}
for i in os.listdir(Path):
try:
img = Image.open(os.path.join(Path,i))
NumpyImg = np.array(img)
folder.append(NumpyImg)
if str(NumpyImg.shape) in stats.keys():
stats[str(NumpyImg.shape)] = stats[str(NumpyImg.shape)] + 1
else:
stats[str(NumpyImg.shape)] = 1
except:
print(str(i), " Cannot open this image")
#img = img.convert(mode = "RGB")
return np.array(folder),stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment