简体   繁体   English

Python - matplotlib - PyQT:将图像复制到剪贴板

[英]Python - matplotlib - PyQT: Copy image to clipboard

I want to copy the current figure of matplotlib to the clipboard: 我想将matplotlib的当前图形复制到剪贴板:

this code doens't work(for testing purposes, I want to save the bitmap to disk later on I will use it as a bitmap-image. I want to start with having a memory-bitmap that I can use with the QT's QImage, QPixmap, QBitmap or some other QT-class). 这段代码不起作用(出于测试目的,我想稍后将位图保存到磁盘上,我会将它用作位图图像。我想从一个内存位图开始,我可以使用QT的QImage, QPixmap,QBitmap或其他一些QT类)。

rgba = self.canvas.buffer_rgba()
im = QImage(self.canvas.buffer_rgba())
print(im.depth())   # --> 0
print(im.save('test.gif',r'GIF')) # ---> False

Any solutions? 有解决方案吗

You're not creating a valid image. 您没有创建有效的图像。

im = QImage(self.canvas.buffer_rgba())

actually ignores the argument you're passing. 实际上忽略了你传递的论点。 canvas.buffer_rgba() returns a memoryview, which none of the available constructors can work with: canvas.buffer_rgba()返回一个内存视图,没有任何可用的构造函数可以使用:

QImage()
QImage(QSize, QImage.Format)
QImage(int, int, QImage.Format)
QImage(str, int, int, QImage.Format)
QImage(sip.voidptr, int, int, QImage.Format)
QImage(str, int, int, int, QImage.Format)
QImage(sip.voidptr, int, int, int, QImage.Format)
QImage(list-of-str)
QImage(QString, str format=None)
QImage(QImage)
QImage(QVariant)

In python there is really only one __init__ method, so type checking has to be done by internal checks. 在python中实际上只有一个__init__方法,因此类型检查必须通过内部检查完成。 QImage(QVariant) is basically the same as QImage(object) (in fact, in python3, that's exactly what it is), so it basically allows you to pass any object, it's just ignored if it can't be converted to a QVariant internally, so the returned QImage will be empty. QImage(QVariant)是基本相同QImage(object) (事实上,在python3,这正是它是什么),因此,它基本上可以让你通过任何物体,它只是被忽略,如果它不能被转换成QVariant在内部,所以返回的QImage将为空。

If you want to construct a image from raw rgba data, you need to specify it's size, as there is no way to get this information from raw data, and also the format. 如果要从原始rgba数据构造图像,则需要指定它的大小,因为无法从原始数据和格式中获取此信息。 This should work: 这应该工作:

size = self.canvas.size()
width, height = size.width(), size.height()
im = QImage(self.canvas.buffer_rgba(), width, height, QImage.Format_ARGB32)
im.save('test.png')

Another way would be to grab the canvas directly as a pixmap: 另一种方法是直接将画布作为像素图抓取:

pixmap = QPixmap.grabWidget(self.canvas)
pixmap.save('test.png')

Save to clipboard: 保存到剪贴板:

QApplication.clipboard().setPixmap(pixmap)

Oh, and you shouldn't use gif , use png . 哦,你不应该使用gif ,使用png Due to it's proprietary nature gif is usually not available as supported output format. 由于它的专有性质, gif通常不支持输出格式。 Check QImageWriter.supportedImageFormats() . 检查QImageWriter.supportedImageFormats()

when working in an interactive Python shell you can use a function like this 在交互式Python shell中工作时,您可以使用这样的函数

def to_clipboard(fig):
  QApplication.clipboard().setImage(QPixmap.grabWidget(fig.canvas).toImage())
  print('figure %d copied to clipboard' % fig.number)

and then use it like to_clipboard(gcf()) to store the current figure. 然后像to_clipboard(gcf())一样使用它来存储当前的数字。

In addition to @mata answer, for the update, since QPixmap.grabWidget is now obsolete, we can use: 除了@mata回答之外,对于更新,由于QPixmap.grabWidget现在已经过时,我们可以使用:

pixmap = self.canvas.grab()
QApplication.clipboard().setPixmap(pixmap)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM