PIL question

Thomas Heller theller at ctypes.org
Fri Nov 16 21:57:29 CET 2007

I'm trying to read an image with PIL, crop several subimages out of it,
and try to combine the subimages again into a combined new one.  This is
a test script, later I want to only several single images into a combined one.

I have to work with 16-bit color windows bitmaps in BMP format.

My input image (input.bmp) IS a 16-color bmp.  The cropped images that
the script saves are saved in 8-bit/256-color format; they look correct.
The combined image is also saved in 8-bit/256-color format, but it is
all black - maybe the palette is missing?

Anyway, I cannot use 256-color images.  How do I save the images in 4-bit/16-color


Here is the code; I'm using Python 2.4 and PIL 1.5:

import Image
img = Image.open("input.bmp")

w, h = img.size
print img.size, img.mode

newimg = Image.new("P", (16*30, 15))

x = 0
i = 1
while x < w:
    this = img.crop((x, 0, x+16, 15))
    this.save("%02d.bmp" % i)
    newimg.paste(this, (x, 0, x+16, 15))
    x += 16
    i += 1



More information about the Python-list mailing list