Any suggestions to speed this up

M

MetalOne

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.

On my computer I can display 3 frames per second of 360x240 data. If
I modify the imageBuf line to imageBuf = imageHdr + rawData*3, then I
can display 40fps. Of course, the image doesn't look right, but that
is the kind of speed that I desire.

I have tried a few things,
1) Building the string using cStringIO.
2) map instead of list comprehension.
3) I tried to get fancy with zip(rawData, rawData, rawData) followed
by string joins.

None of these had any speed improvement and 3) was much slower.

I am hoping not to have to write an extension module in "C".
 
T

Terry Reedy

MetalOne said:
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.

I would look at PIL to see it it had a builtin function to do this and
then Numerical Python.

TJR
 
C

Christos TZOTZIOY Georgiou

I would look at PIL to see it it had a builtin function to do this and
then Numerical Python.

I believe PIL Image.convert and Image.getdata provide all the necessary
functionality.
 
F

Fredrik Lundh

MetalOne said:
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>
 
M

MetalOne

Thanks.
Your suggestion worked with a slight tweak to the code.

def buildBitmap2(rawData, w, h):
im = Image.fromstring("L", (w, h), rawData)
im = im.convert("RGB")
file = cStringIO.StringIO()
im.save(file, "PPM")
file.seek(0)
return wxBitmapFromImage(wxImageFromStream(file) )

On my home computer, my frame rate went from 7fps to 27fps.
I had glanced at PIL earlier in the week and did not think it would
work. Your post gave me some confidence that it would, so I looked
into it a little more. PIL looks to be quite cool.

I tried getting your second suggestion with the Dibs to work, but I
can't seem to make them work with wxWindows. I can't get access to
the underlying device context handle.

I also tried playing around with Array('c') and numarray. Iterating
over these sequences was no faster than with list. At least, it did
not change the frame rate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top