Color count in PIL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Larry

    Color count in PIL

    I've been walking up, down, and around instances in the object model
    of PIL trying to figure out how to easily calculate how many unique
    colors are used in an image, specifically a bitmap (mode "P"). Am I
    missing something?

    Thanks python friends!
  • Larry

    #2
    Re: Color count in PIL

    wrbt@email.com (Larry) wrote in message news:<2ec1bc1c. 0401271443.15f5 6742@posting.go ogle.com>...[color=blue]
    > I've been walking up, down, and around instances in the object model
    > of PIL trying to figure out how to easily calculate how many unique
    > colors are used in an image, specifically a bitmap (mode "P"). Am I
    > missing something?
    >
    > Thanks python friends![/color]

    So far I have: colors=len(sets .Set(list(im.ge tdata())))

    That makes me want to throw up.

    Comment

    • David M. Cooke

      #3
      Re: Color count in PIL

      At some point, wrbt@email.com (Larry) wrote:
      [color=blue]
      > wrbt@email.com (Larry) wrote in message news:<2ec1bc1c. 0401271443.15f5 6742@posting.go ogle.com>...[color=green]
      >> I've been walking up, down, and around instances in the object model
      >> of PIL trying to figure out how to easily calculate how many unique
      >> colors are used in an image, specifically a bitmap (mode "P"). Am I
      >> missing something?
      >>
      >> Thanks python friends![/color]
      >
      > So far I have: colors=len(sets .Set(list(im.ge tdata())))
      >
      > That makes me want to throw up.[/color]

      Probably very inefficient. Have a look at im.histogram(). Something
      like:

      colours = sum( [ 1 for h in im.histogram() if h != 0 ] )

      --
      |>|\/|<
      /--------------------------------------------------------------------------\
      |David M. Cooke
      |cookedm(at)phy sics(dot)mcmast er(dot)ca

      Comment

      • Fredrik Lundh

        #4
        Re: Color count in PIL

        "Larry" <wrbt@email.com > wrote:
        [color=blue]
        > I've been walking up, down, and around instances in the object model
        > of PIL trying to figure out how to easily calculate how many unique
        > colors are used in an image, specifically a bitmap (mode "P"). Am I
        > missing something?[/color]

        colors = len(filter(None , im.histogram()) )

        </F>




        Comment

        Working...