# [ACCEPTED]-Find images with similar color palette with Python-colors

Build a color histogram for each image. Then 22 when you want to match an image to the collection, simply 21 order the list by how close their histogram 20 is to your selected image's histogram.

The 19 number of buckets will depend on how accurate 18 you want to be. The type of data combined 17 to make a bucket will define how you prioritize 16 your search.

For example, if you are most 15 interested in hue, then you can define which 14 bucket your each individual pixel of the 13 image goes into as:

```
def bucket_from_pixel(r, g, b):
hue = hue_from_rgb(r, g, b) # [0, 360)
return (hue * NUM_BUCKETS) / 360
```

If you also want a general 12 matcher, then you can pick the bucket based 11 upon the full RGB value.

Using PIL, you can 10 use the built-in `histogram`

function. The "closeness" histograms 9 can be calculated using any distance measure 8 you want. For example, an L1 distance could 7 be:

```
hist_sel = normalize(sel.histogram())
hist = normalize(o.histogram()) # These normalized histograms should be stored
dist = sum([abs(x) for x in (hist_sel - hist)])
```

an L2 would be:

```
dist = sqrt(sum([x*x for x in (hist_sel - hist)]))
```

`Normalize`

just forces the sum of 6 the histogram to equal some constant value 5 (1.0 works fine). This is important so that 4 large images can be correctly compared to 3 small images. If you're going to use L1 2 distances, then you should use an L1 measure 1 in `normalize`

. If L2, then L2.

More Related questions