The problem is that it creates a histogram that has no values (or indeed None values), where there is no corresponding pixel value.
i.e. when you find the difference of two images, the resulting image does not have pixels that are, say, at a distance of 43 units, so h [43] = None.
Later, you try to access the number of pixels at each brightness in the range (256) and put it in a square, which makes him wonder what None ** 2 should have.
Consider changing range(256) to h.keys() .
In addition, you use h to mean two different things, think about changing the name of one or, better yet, both of them for meaningful names.
source share