ImageItem API change?

251 views
Skip to first unread message

Andrew Hakman

unread,
Nov 15, 2022, 2:45:37 AM11/15/22
to pyqtgraph
I recently upgraded my machine and am now using QT 6.3.1 and pyqtgraph 0.13.1-3

There seems to be an API change with ImageItem.scale() that breaks existing code that was working perfectly before the upgrade.

As best I can tell, scale() is inherited from QGraphicsItem, and at some point in the past, it used to take 2 arguments x_scale,y_scale, but now takes none.

There are a few examples around of setting the scale of the image based on the number of points in my data, like this one from here: https://www.mail-archive.com/pyqt...@googlegroups.com/msg00740.html

self.image = pg.ImageItem(np.zeros((1, 1)))
self.plot.addItem(self.image)
self.image.setImage(image)
# x-axis coordinate values are wavelengths (eg, 450--750)
# y-axis coordinates are simply the camera pixel numbers (0--199)
self.xlabels = np.arange(450, 750)
# Translate and scale the ImageItem to match the x-axis labels
self.image.translate(self.xlabels[0], 0)
self.image.scale((self.xlabels[-1] - self.xlabels[0])/len(self.xlabels), 1)

You can find various examples of ImageItem.scale(x_scale, y_scale) when googling a bit.

But now after the update to either QT or pyqtgraph (or both), that last line throws the error

TypeError: ImageItem.scale() takes no arguments (2 given)

So what's the new way to do this now? Is it using QtGui.QTransform, and then assigning the transform to the ImageItem?

I can't even find an older version of the QT API or the PyQTGraph API that shows a scale function that takes any arguments (so that's why I'm unsure which class the scale function used to even come from).

Andrew Hakman

unread,
Nov 15, 2022, 3:00:04 AM11/15/22
to pyqtgraph
I guess to answer my own question, yes, using QtGui.QTransform seems to be the way now:

old way:
            self.Img = pg.ImageItem()
            self.Img.scale((data_storage.x[-1] - data_storage.x[0]) / len(data_storage.x), 1)
            self.plot.clear()
            self.plot.addItem(self.waterfallImg)
which now fails with
TypeError: ImageItem.scale() takes no arguments (2 given)

new way:
            self.Img = pg.ImageItem()
            tr = QtGui.QTransform()
            tr.scale((data_storage.x[-1] - data_storage.x[0]) / len(data_storage.x), 1)
            self.waterfallImg.setTransform(tr)
            self.plot.clear()
            self.plot.addItem(self.waterfallImg)
Works now the way it used to

I don't see this documented anywhere as an API change

Martin Chase

unread,
Nov 15, 2022, 8:56:30 AM11/15/22
to pyqt...@googlegroups.com
Hey,

Yeah, that behavior got deprecated out of Qt (it lives on QGraphicsItem) a couple of years ago. To set independent x and y scales now, you have to use a transform ala:

    setTransform(QtGui.QTransform.fromScale(x_scale, y_scale))

If the x and y are the same, you can instead use:

    setScale(xy_scale)

Cheers,
 - Martin
Reply all
Reply to author
Forward
0 new messages