Strech image to window size in pyqtgraph

2.2k views Asked by At

I'm currently trying to create a PyQtGraph gui to plot an image repeatedly as new data comes in using code similar to this:

self.app = QtGui.QApplication([])
self.win = QtGui.QMainWindow()
self.win.resize(800, 600)
self.imv = pg.ImageView()
self.win.setCentralWidget(self.imv)
self.win.setWindowTitle('My Title')
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.check_for_new_data_and_replot)
self.timer.start(100)
self.win.show()

then each time I get new data I draw the image:

self.imv.setImage(self.data_array)

One problem I'm running into is that my data array usually has a skewed aspect ratio, i.e. it is usually really "tall and skinny" or "short and fat", and the image that is plotted has the same proportions.

Is there a way to stretch the image to fit the window? I've looked through the documentation for ImageView and ImageItem, but can't find what I need. (Perhaps it is there, but I am having trouble identifying it.)

2

There are 2 answers

0
AudioBubble On BEST ANSWER

I figured it out-- using the lower-level ImageItem class displayed the image in a way that stretched to fit the window size:

self.app = QtGui.QApplication([])
self.win = pg.GraphicsLayoutWidget()
self.win.resize(800, 600)
self.img = pg.ImageItem()
self.plot = self.win.addPlot()
self.plot.addItem(self.img)
self.win.setWindowTitle('My Title')
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.check_for_new_data_and_replot)
self.timer.start(100)
self.win.show()

And to update the image data:

self.img.setImage(self.data_array)

This also lets me display axis scales on the sides, which was a feature I wanted as well.

0
Vera On

If you still want to keep the full ImageView functionality you can embed your ImageView in a PlotItem:

self.plot = pg.PlotItem()
self.imv = pg.ImageView(view=self.plot)

This will also give you axis scales. You can then use

self.plot.setAspectLocked(False)

to stretch the image to fit the window size.