So i want to make a histogram from the pixelvalues of an image and it just takes forever.
my histogram function is the following:
def histogram(pixelcount):
'''
displays the histogram of the current image
'''
fig=go.Figure()
fig.add_trace(go.Histogram(x=pixelcount,
xbins=dict(size=5)))
return fig
The input is a list created this way:
imgy=Image.open(img, 'r')
pix_val=list(imgy.getdata())
#i dont care about 0 values
pix_count=[x for sets in pix_val for x in sets if x>0]
and creating the histogram takes several minutes.
I do not understand why. Am i passing the data wrong, or is there a faster way to make a histogram?
Sure the list is approx. 1.8 mio elements long, but the whole operation
imgy=Image.open(img, 'r')
pix_val=list(imgy.getdata())
pix_count=[x for sets in pix_val for x in sets if x>0]
for i in pix_val_flat:
if i in pixel_dict.keys():
pixel_dict[i]+=1
else:
pixel_dict.update({i:1})
takes approximately a second, while creating the histogram takes several minutes.