A histogram is the quickest summary of an image's tone: count how many pixels fall into each brightness band and you can see at a glance whether a photo is underexposed, blown out, or sitting comfortably in the middle.
Task: write histogram(pixels, bins) returning a list of bins counts.
pixels is a 2D grid of integers from 0 to 255.0–255 into bins equal-width bands and count how many pixels land in each.In Python that's p * bins // 256 — integer division, no floats needed.
bins entries, including zeros for empty bands, and they sum to the total pixel count.The 256 matters. There are 256 possible values (0 through 255), so dividing by 256 — rather than 255 — puts the largest possible pixel in the last band and nothing beyond it. Divide by 255 instead and a pixel of exactly 255 lands at index bins, one past the end of your list, for an IndexError that only ever appears on images containing pure white.
Multiply before dividing, too: p * bins // 256 is exact integer arithmetic, while p // (256 // bins) breaks as soon as bins isn't a power of two.