[Image-SIG] How to convert mode 'RGB' into 'P'

Bjoern Platzen bplatzen at sosnetz.de
Fri Oct 10 09:06:09 EDT 2003

Hello List,

I'm a Newbie to the PIL and I'm trying to make a composite of 2 
PNG-files using a 3rd as mask. PNG1 is a map and PNG2 is a Border that 
should be drawn on top of the map.
The only way to do this, seemed to be by generating a new Image ('RGB', 
(sizeX, sizeY), (65535, 65535, 65535)) and paste my PNG1 into it. Then 
I generate a new image with Image.composite(PNG2, NewImage, mask).

The saved Image looks good, but it has about 220 kb and it's mode is 

If I do a Image.composite(PNG2, PNG1, mask), the image has only 94 kb 
and it's mode is 'P'. But in this case, the border (from PNG2) is not 
rendered correctly.

Here's the code-snippet:
import Image
import PngImagePlugin

Image._initialized = 1

##Open all the images, we need...
mymap = Image.open('map800.png')
mask = Image.open('rahmen.png')
myborder = Image.open('rahmen_t.png')
##build a new image as the base of the new image...
white = Image.new('RGB', mymap.size,(65535,65535,65535))
##paste the map onto the white background
##building the output-image by composing the Border with the new Image,
##using a transparent Border-Image as the transparency-mask...
outimg = Image.composite(myborder, white, mask)
outimg2 = Image.composite(myborder, mymap, mask)
outimg.save('combined.png', 'PNG', optimize=1)
outimg2.save('combined2.png', 'PNG', optimize=1)

I tried outimg.convert('P') but it had no effect. The image was still in 

Is there anyway to do this? I think my images will become smaller when 
they are in mode 'P'.

Thanks in advance,


small office solutions
info at sosnetz.de  -  http://www.sosnetz.de

More information about the Image-SIG mailing list