[Image-SIG] Initializing a 'P' image

Chris Barker chris.barker at noaa.gov
Tue Jun 19 21:24:49 CEST 2012


HI folks,

I'm creating, then drawing to, a "P" paletted image.

I like how ImageDraw will let you set the colors as you drawm and it
will automaticaly get added to teh palette.

However, I can't see how to initialize the image with a given
background (fill) color:

In [35]: Image.new('P', (10,10), color=(255,255,255))

TypeError: an integer is required

I can set color to 0  (Or, presumabley any other 8-bit integer), but
then there is no color added to the palette, so it will get whatever I
happend to draw first.

So I've ended up doing this:

        self.image = Image.new('P', size, color=0)
        drawer = ImageDraw.Draw(self.image) # couldn't find a better
way to initilize the colors right.
        drawer.rectangle(((0,0), size), fill=self.background_color)

but that sure feels kludgy -- have I missed something?

Of course, I could manage the palette myself, but that's uglier...

-Chris







-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov


More information about the Image-SIG mailing list