[PyQt] Pixel pushing performance

Giovanni Bajo rasky at develer.com
Thu May 8 00:38:34 BST 2008


On Thu, 2008-05-08 at 01:16 +0200, Håkon Bertheussen wrote:
> Hi,
> 
> I'm just getting started with PyQt, trying to implement a fairly complex
> widget in python. Unfortunately, it cannot easily be painted using
> higher level drawing primitives, meaning that I have to set individual
> pixels. I realize that this obviously has a lot of overhead compared to
> a C++ implementation, but if possible I'd like to stay in Python. Here's
> what I'm doing now:
> 
>      def paintEvent(self, event):
>          canvas = QtGui.QImage(event.rect().size(), 		
>                                QtGui.QImage.Format_RGB32)
>          for i in xrange(0, canvas.height()):
>              for j in xrange(0, canvas.width()):
>                  canvas.setPixel(j, i, 123456)
>          painter = QtGui.QPainter()
>          painter.begin(self)
>          painter.fillRect(event.rect(), QtGui.QBrush(canvas))
>          painter.end()
> 
> Obviously, in my real application the inner statement in the loop is a
> bit more complex, but even the above example is slow when the widget is
> fairly large (e.g. 300x300px). Is there a way to speed this up?

To avoid the slowness, you must find a way to avoid looping through all
pixels. You can't simply have a real-time 300x300 loop in Python, at
all.

The .setPixel() itself can probably be avoided by constucting a numpy
array (or similar object with the buffer interface) and passing it to
QImage (and might be needed to ask Phil to add support to it, if not
ready yet). But you still fill that numpy array one element at a time,
nothing changes.
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com




More information about the PyQt mailing list