473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Any suggestions to speed this up

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".
Jul 18 '05 #1
6 1948

"MetalOne" <jc*@iteris.com> wrote in message
news:92**************************@posting.google.c om...
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
Jul 18 '05 #2
On Tue, 4 Nov 2003 17:16:07 -0500, rumours say that "Terry Reedy"
<tj*****@udel.edu> might have written:
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.


I believe PIL Image.convert and Image.getdata provide all the necessary
functionality.
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #3
"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>


Jul 18 '05 #4
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.
Jul 18 '05 #5
Hello MetalOne,
On my home computer, my frame rate went from 7fps to 27fps.

Have you tried Psyco (http://psyco.sf.net)? This might give you some
more speedup without rewriting any code.

HTH.
Miki
Jul 18 '05 #6
> Have you tried Psyco (http://psyco.sf.net)? This might give you some
more speedup without rewriting any code.


I gave pysco a try and had no improvement in frame rate.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ideasman | last post by:
Hi I have a made a script that process normals for a flat shaded 3D mesh's. It compares every vert with every other vert to look for verts that can share normals and It takes ages. I'm not...
0
by: Matt W | last post by:
Hi all, I'm planning to use MySQL's full-text search for my forum system (possibly 5+ million posts). I've been playing with it a lot lately to see the performance and functionality and have...
2
by: Torfi Sackbatten | last post by:
Hi Everyone, I´m asked to "speed up" a keyword search based on MySQL. The material i´m given to work with is a quite large MySQL table with 1.5 mio rows, defined something like: CREATE TABLE...
8
by: noid droid | last post by:
Hi. I posted yesterday asking if C# lived up to the hype. Thus far the feedback has been all positive. (Thanks.) Can anyone suggest GOOD books for learning C# and the Visual Studio .NET IDE? ...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
16
by: Ding Lei | last post by:
Dear fellows, I am currently a Java programmer, using it for around 3 years, & felt quite bored with it. IMHO, Java is too strict on lots of things, unlike Perl, There is usually only one or two...
1
by: Brian Basquille | last post by:
Hello all. Have been working on the Air Hockey game on and off for a couple of weeks now.. but have had plenty of other assignments to keep me busy along with it. But i would like some...
5
by: Brian P. Hammer | last post by:
I have data from multiple SQL tables, some of the tables will only have one row, while others multiple rows. I load a bunch of data from the various tables and add them to a third party grid. With...
0
by: pmouse | last post by:
Hi Guys, I've written a templated lru cache based on the SGI version of STL. To use the class, simply write: lru_cache<key_type, data_type, cache_length, custom_containercache; cache.insert( key,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.