[Image-SIG] Palette and alpha

Andres Moreira elkpichico at gmail.com
Mon Oct 26 19:49:08 CET 2009


Hi all, 

 I'm using the below code to extract colors, but now, I need to extract colours with 
 the alpha channel. 

 I've read the PIL Doc and googling a lot, and I didn't find any information of 
 how to load a RGBA palette, so the extractcolours method can couple with image
 alpha sections.
 
    def load_palette(colour_map):
        mapim = Image.open(colour_map)
        palette = [color[:3] for _, color in mapim.getcolors()]

        # Flatten colors to be accepted by PIL
        palette = tuple(reduce(lambda a, b: a+b, palette))

        # create palette
        self.impalette = Image.new("P", (1, 1)) 
        self.impalette.putpalette(palette)

    def extractcolours(filename):
        pilimage = Image.open(filename)
        im_quantized = pilimage.convert("RGB").quantize(palette=self.impalette)
        im_in_rgba = im_quantized.convert("RGBA", dither=self.dither)
        return im_in_rgba.getcolors()
     
 What I've get with this code for images with transparency is the list of colours, 
 and the one colour : (0,0,0,255) (and all the ohters colors with alpha = 255)

 The convert tool from Imagemagick, return me the list of colours with all the 
 non-(0,0,0) colours with alpha channel = 255 and (0,0,0,0). 
 So with this list, I can couple with the aplha sections removed from my results.

 It's possible to do this with PIL?

 Thanks a lot.

Andres


More information about the Image-SIG mailing list