Reputation: 161
So I am trying to cycle through a set of images in a directory and I'm looking to save the output of each calculate as it's own variable, in this case d1, d2, and d3. For some reason, this only outputs d3 and none of the other values. Any help regarding what is wrong would be greatly appreciated!
filelist = ['IMG_1.jpg','IMG_2.jpg', 'IMG_3.jpg']
for imagefile in filelist:
for i in range(1,4):
t=Image.open(imagefile).convert('L')
arr = array(t) #Convert test image into an array
f = arr + c #Add the corrective factor to the array with the UBM
f[f > 150] = 0
value = np.sum(f) #Sum elements in array
con = np.count_nonzero(f) #Count number of nonzero elements
arraysDict = {}
arraysDict['d{0}'.format(i)] = value/con
print arraysDict
If I do it this way (below), it prints each value for d1, d2, and d3 but they are the same for some reason.
filelist = ['IMG_1604.jpg','IMG_1605.jpg', 'IMG_1606.jpg']
for imagefile in filelist:
t=Image.open(imagefile).convert('L')
arr = array(t) #Convert test image into an array
f = arr + c #Add the corrective factor to the array with the UBM
f[f > 150] = 0
value = np.sum(f) #Sum elements in array
con = np.count_nonzero(f) #Count number of nonzero elements
arraysDict = {}
for i in range(1,4):
arraysDict['d{0}'.format(i)] = value/con
q = arraysDict.values()
print q
Upvotes: 0
Views: 252
Reputation:
You are initialising the arraysDict = {}
every time inside the for
loop. Which will clear the old data. Just initialize the arraysDict = {}
outside the for loop.
filelist = ['IMG_1.jpg','IMG_2.jpg', 'IMG_3.jpg']
arraysDict = {}
value = {}
con = {}
i = 1
for imagefile in filelist:
t=Image.open(imagefile).convert('L')
arr = array(t) #Convert test image into an array
f = arr + c #Add the corrective factor to the array with the UBM
f[f > 150] = 0
value[i] = np.sum(f) #Sum elements in array
con[i] = np.count_nonzero(f) #Count number of nonzero elements
i += 1
arraysDict = {}
for i in range(1,4):
arraysDict['d{0}'.format(i)] = value[i]/con[i]
q = arraysDict.values()
print q
Upvotes: 2