Export full contents of window or layout

41 views
Skip to first unread message

Matt Huszagh

unread,
Jun 17, 2020, 6:35:47 PM6/17/20
to pyqtgraph
I'm attempting to export the contents of a full window or layout. The layout consists of a central ImageView with x and y axes. I'm able to export the image but then it ignores the axes and y inversion. For instance, something like this will export the central image.

```
from pyqtgraph.Qt import QtGui
import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np

app = QtGui.QApplication([])
win = QtGui.QMainWindow()
imv = pg.ImageView(view=pg.PlotItem())
img_view = imv.getView()
img_view.invertY(False)
img_view.setLimits(yMin=0, yMax=512)
img_view.getAxis("left").setScale(0.5)
win.setCentralWidget(imv)
win.show()
win.setWindowTitle("Range Plot")
imv.setPredefinedGradient("flame")
imv.setImage(np.zeros((2000, 513)), xvals=[i for i in range(2000)])
imv.setLevels(0, 100)
app.processEvents()
imv.export("plot.png")
```

How can I get this to export the full window as an image (preserving axes, aspect ratios, y-inversion, etc.)? Is there some window class or layout on which I can call `scene()`? Thanks.

Kenneth Lyons

unread,
Jun 19, 2020, 1:14:08 AM6/19/20
to pyqtgraph
One option I can think of is that QWidget (hence QMainWindow) has a grab() method render to a pixmap which can then be saved to a file, so you could use that to generate a "screenshot" of the whole window. Does this do what you're after?

pixmap = win.grab()
pixmap
.save("plot.png")

(in place of imv.export("plot.png"))

It doesn't offer much in terms of options like DPI (you could blow it up by specifying a large window size, but the axis labels won't scale with it). There may be more flexibility with render(), I haven't tried it.

Matt Huszagh

unread,
Jun 19, 2020, 2:13:52 PM6/19/20
to pyqtgraph
I wasn't able to get grab() to work. I received an error that QMainWindow does not have a grab method. However, render() did the trick. Thanks for pointing that out! Here's the code I used, in case anyone else comes across this.

pixmap = QtGui.QPixmap(win.size())
win
.render(pixmap)
pixmap
.save("plot.png")


This appears to give me an exact image of what I saw in the window.
Reply all
Reply to author
Forward
0 new messages