Python - if statements involving numpy arrays -
i'm opening image , converting array of rgba values. here's code:
img = image.open('17112013.png') imgarray = array(img) l = imgarray[:,1,1] m = imgarray[1,:,1] n = imgarray[1,1,:] bluecount = 0 redcount = 0 flag = 0 in range(0,len(l)): j in range(0,len(m)): if (imgarray[i,j] == [255,255,255,255]): pass if (imgarray[i,j] == [0,0,0,255] , flag == 0): pass if (imgarray[i,j] == [255,0,0,255] , flag == 0): pass if (imgarray[i,j] == [0,0,255,255]): flag = 1 bluecount = bluecount + 1 if (imgarray[i,j] == [255,0,0,255] , flag == 1): redcount = redcount + 1 if (imgarray[i,j] == [0,0,0,255] , flag == 1): flag = 0 if (imgarray[i,j] == [255,255,255,255] , flag == 1): flag = 0
if run such, receive following:
valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all()
i understand need use these 'if' statements containing numpy arrays; however, unsure of correct syntax implement this. being stupid, appreciated.
you want like:
if np.all(imgarray[i,j] == [255,255,255,255]):
if particular slice of imgarray
indeed [255,255,255,255]
, equality comparison return array([ true, true, true, true], dtype=bool)
. can't expect if
know boolean array directly, have discovered, function numpy.all(x)
returns true
if of values in array x
equivalent true
, false
otherwise.
Comments
Post a Comment