Last active
March 18, 2019 14:43
-
-
Save Shivam60/ea0032bb20f901a52737466f8df18045 to your computer and use it in GitHub Desktop.
ConvertImagesToNumpyArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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