In a pyQt application, I'm copying matplotlib figures (self.canvas
) to a QClipboard instance using either
cb = QClipboard(self)
img = QImage(self.canvas.grab())
cb.setImage(img)
or
img = QPixmap(self.canvas.grab())
self.cb.setPixmap(img)
Both work well, however, I haven't managed to control the resolution of the exported image. This would be possible by creating and exporting a temporary file, however, this is slow and has potential problems due to file system restrictions:
self.canvas.figure.savefig(self.temp_file, dpi = 300, type = 'png')
temp_img = QImage(self.temp_file)
cb.setImage(temp_img)
So, is there a way to set the resolution of the copied image without taking a detour through the file system?
-------------------------------------
Edit: I just found out that the above doesn't work under pyqt4. Instead, you can use
img = QPixmap.grabWidget(self.canvas)
self.cb.setPixmap(img)
-------------------------------------
Edit: Another solution that nearly works is the following piece of code, unfortunately it changes the colors (back to matplotlib defaults?):
# construct image from raw rgba data, this changes the colormap:
size = self.canvas.size()
width, height = size.width(), size.height()
im = QImage(self.canvas.buffer_rgba(), width, height, QImage.Format_ARGB32)
self.cb.setImage(im)