list conversion question

Paul McGuire ptmcg at austin.rr._bogus_.com
Sat Sep 4 05:37:35 CEST 2004


"Kent Tenney" <kent at springfed.com> wrote in message
news:mailman.2860.1094267481.5135.python-list at python.org...
> Howdy,
>
> I am using PIL, and would like to
> make a list of the pixel values
> in an image, sorted by quantity of
> pixels with that value.
>
> im.histogram() returns a list which
> is a pixel count for each brightness value.
>
> So, I want to sort the histogram, and have
> the resulting list contain
> the indexes instead of the counts.
>
> This seems like it'd be a fairly common
> task, but I don't know what to call
> it to look for guidance.
>
> Any help is appreciated.
>
> Thanks,
> Kent
>
Assuming im.histogram() returns a list like [ 0, 1, 0, 5, 43, etc. ] how
about:

hist = [ 0, 1, 0, 5, 43 ]
values = [ i for i in enumerate(hist)]
values.sort(lambda a,b: cmp(b[1],a[1]))
indexes = [ a for a,b in values ]

-- Paul





More information about the Python-list mailing list