[Image-SIG] Float PIL image to bitmap

John Hunter jdhunter@ace.bsd.uchicago.edu
Thu, 15 Nov 2001 13:50:50 -0600


I have a grayscale PIL image created from a file of 16 bit floats:

  data = open(inFile, 'rb').read(131072) 
  im = Image.fromstring( "F", (256,256), data, \
                       "raw", "F;16B", 0, 1)

I would like to use this in wxPython as a bitmap.  Currently I am
using the wxImage method ConvertToBitmap after first converting the
PIL image to a wxImage using Cliff Wells' PILToWx (see below)

  bmp = PILToWX( pil ).ConvertToBitmap()

but this isn't very efficient.  Is there a way to get the bitmap
directly from the float PIL image without having to go through the
intermediary wxImage?  I tried

  bmp = pil.tobitmap()

but I get the error

ValueError: not a bitmap

Thanks,
John Hunter

def PILToWX(image):
    "convert a PIL image to a wxImage"
    if (image.mode != 'RGB'):
        image = image.convert('RGB')
    imageData = image.tostring('raw', 'RGB')
    imageWx = wxEmptyImage(image.size[0], image.size[1])
    imageWx.SetData(imageData)
    return imageWx