Any suggestions to speed this up

Fredrik Lundh fredrik at pythonware.com
Fri Nov 7 02:14:38 EST 2003


"MetalOne" wrote:

> def buildBitmap(rawData, w, h, maxColorVal):
>     """ppm format, http://netpbm.sourceforge.net/doc/ppm.html
>        Constructs a wxBitmap.  The <rawData> input is a raw
>        grayscale image, 8 bits per pixel."""
>     imageHdr = "P6\r%d %d\r%d\r" % (w, h, maxColorVal)
>     #expand grayscale to rgb
>     imageBuf = imageHdr + string.join([v*3 for v in rawData], "")
>     return wxBitmapFromImage( wxImageFromStream(
> cStringIO.StringIO(imageBuf) ))
>
> Virtually all the time is consumed expanding the 1 byte of grayscale
> data into 3 bytes to get RGB.  For example, V=128 is converted to
> R=128,G=128,B=128.

this might work:

def buildBitmap(rawData, w, h, maxColorVal=255):
     """pgm format, http://netpbm.sourceforge.net/doc/pgm.html
        Constructs a wxBitmap.  The <rawData> input is a raw
        grayscale image, 8 bits per pixel."""
     imageHdr = "P5\r%d %d\r%d\r" % (w, h, maxColorVal)
     return wxBitmapFromImage( wxImageFromStream(
        cStringIO.StringIO(imageBuf) ))

if it doesn't, use PIL:

    http://www.pythonware.com/products/pil/index.htm

something like this might work (untested; tweak if necessary):

from PIL import Image

def buildBitmap(rawData, w, h):
    im = Image.fromstring("L", (w, h), rawData)
    im = im.convert("RGB")
    file = cStringIO.cStringIO()
    im.save(file)
    file.seek(0) # rewind
    return wxBitmapFromImage(wxImageImageFromStream(file))

also see:

    http://effbot.org/zone/pil-image.htm

if you're running under Windows, you may be able to get better
performance by wrapping the PIL image in a Dib object, and
copying the Dib directly to screen; see:

    http://effbot.org/zone/pil-imagewin.htm

</F>








More information about the Python-list mailing list