[PyQt] Pixel Manipulation Very Slow?

Henning Schröder post at henning-schroeder.de
Fri Aug 20 14:27:17 BST 2010

On Fri, Aug 20, 2010 at 12:21 PM, zhang jian <zhangmdev at gmail.com> wrote:
> Hello,
> This is my first post. I want to write a simply app layering several images
> each with different weighting. This problem is pixel manipulation by QImage
> appears to be quite slow.
The problem is that name look-ups take a lot of time in Python,
because every method is
accessed by its name (a string in a huge hash table).

You can get some speed improvements in loops by creating shorter references
(with less dots because less namespace hash tables have to be searched through)

           QColor = QtGui.QColor # or: from PyQt4.QtGui import QColor
           setPixel = image.setPIxel
           pixel = image.pixel
           w = image.width()
           h = image.height()
           for j in xrange(h): # xrange instead of range might be better
              for i in xrange(w):
                   col = QColor(pixel(i, j))
                   setPixel(i, j, col.rgb())

You could also use numpy for your problem. Use the data from
Qimage.bits to create a numpy array, do the transformations and create
a new QImage with QImage.fromData.


More information about the PyQt mailing list