PyQt4 - QWidget 保存为图像 [英] PyQt4 - QWidget save as image

查看:42
本文介绍了PyQt4 - QWidget 保存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 以编程方式将 PyQt4 中的 QWidget 保存为图像(任何格式都可以 - PNG、PDF、JPEF、GIF...等)

I'm trying to use Python to programmatically save a QWidget in PyQt4 as an image (any format would be fine - PNG, PDF, JPEF, GIF...etc)

我认为这会非常简单,但实际上我无法在网上找到任何关于它的信息.有人能指出我正确的方向吗?

I thought this would be very straightforward, but I actually wasn't able to find anything on the web about it. Can someone point me in the right direction?

明确地说,我正在尝试这样做

To be clear, I'm trying to do this

gui = <SOME QMainWindow>
gui.show()  # this displays the gui. it's useful, but what i need is to save the image
gui.save("image.png")    ## How do I do this?  

推荐答案

您可以使用 QPixmap.grabWindow() 方法来做到这一点.

You can do this using the QPixmap.grabWindow() method.

来自文档:

抓取窗口窗口的内容并从中制作像素图.返回像素图.

Grabs the contents of the window window and makes a pixmap out of it. Returns the pixmap.

参数 (x, y) 指定窗口中的偏移量,而 (w, h)指定要复制的区域的宽度和高度.

The arguments (x, y) specify the offset in the window, whereas (w, h) specify the width and height of the area to be copied.

如果 w 为负数,函数将所有内容复制到右边框的窗户.如果 h 为负,则函数将所有内容复制到窗口底部.

If w is negative, the function copies everything to the right border of the window. If h is negative, the function copies everything to the bottom of the window.

请注意,grabWindow() 从屏幕上抓取像素,而不是从窗户.如果另一个窗口部分或完全覆盖在该窗口上你抓取,你也会从上层窗口获得像素.

Note that grabWindow() grabs pixels from the screen, not from the window. If there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too.

还要注意鼠标光标一般不会被抓取.

Note also that the mouse cursor is generally not grabbed.

我们使用窗口标识符而不是 QWidget 的原因是为了启用抓取不属于应用程序的窗口,窗口系统框架等.

The reason we use a window identifier and not a QWidget is to enable grabbing of windows that are not part of the application, window system frames, and so on.

警告:抓取屏幕外的区域通常是不安全的.这取决于底层的窗口系统.

Warning: Grabbing an area outside the screen is not safe in general. This depends on the underlying window system.

警告:仅 X11:如果窗口与根窗口的深度不同另一个窗口部分或完全遮住了你抓住的那个,您不会从上覆窗口获得像素.的比赛像素图中的模糊区域未定义且未初始化.

Warning: X11 only: If window is not the same depth as the root window and another window partially or entirely obscures the one you grab, you will not get pixels from the overlying window. The contests of the obscured areas in the pixmap are undefined and uninitialized.

示例代码:

import sys
from PyQt4.QtGui import *

filename = 'Screenshot.jpg'
app = QApplication(sys.argv)
widget = QWidget()
widget.setLayout(QVBoxLayout())
label = QLabel()
widget.layout().addWidget(label)

def take_screenshot():
    p = QPixmap.grabWindow(widget.winId())
    p.save(filename, 'jpg')

widget.layout().addWidget(QPushButton('take screenshot', clicked=take_screenshot))

widget.show()
app.exec_()

这将产生一个如下所示的窗口:

This will produce a window that looks like this:

当您单击该按钮时,它会在当前目录中创建一个名为 Screenshot.jpg 的文件.所述图像将如下所示(注意窗口框架丢失):

When you click the button, it will create a file named Screenshot.jpg in the current directory. Said image will look like this (notice the window frame is missing):

这篇关于PyQt4 - QWidget 保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆