I'm using the following code to iterate over images in a folder and to save them in a dictionary with the filenames as keys, but it's blowing up memory requirements quickly even though the images in the folder should easily fit into memory. Why is this, and how can I fix it?
def make_image_dict(folders):
image_dict={}
for folder in folders:
files = os.listdir(folder)
print ("Loading images in folder:", folder)
for file in files:
try:
image=ndimage.imread(folder+'/'+file)
if file.endswith('.png'):
image_name = str(file)[:-4]
image_dict[image_name]=image
except IOError as e:
print (e)
return image_dict