maya screenshot pyqt截屏

流程中的截图功能是必不可少的,实现的方法也很多。但是原理都是将一个动态桌面生成静态页面,然后裁剪选择的区域.之前基于pyqt写过一个截图的功能。现在跟大家分享一下,你可以任意更改代码,也可以写个界面把截图功能嵌入进去,当然如果可以请保留作者的信息.请留意博客更新,会不定期分享更多的代码便于大家学习!
由于工作时间没时间更新代码,也没优化,把之前写的代码附上,运行以下代码需要事先安装好pyqt.如果没有安装可以试试从maya2014 以上的PySide里导入QtGui和QtCore,我没测试PySide,不过应该可以。如果有什么问题请留言。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#-------------------------------------
#Authors: Deng Zhenggang - zg@cgdzg.com , www.cgdzg.com
import sys
try:
    from PyQt4 import QtGui,QtCore
except ImportError:
    raise ImportError("You need PyQt installed for using this tool.")
    sys.exit()
 
class Rubb(QtGui.QLabel):
    def __init__(self,parent=None,name=None):
        super(Rubb,self).__init__(parent)
        self.name = name
        self.createWidgets()
        # install event filter
        self.fullScreenLabel.installEventFilter(self)  
 
    def eventFilter(self,widget,event):       
        if widget != self.fullScreenLabel:
            return QtGui.QLabel.eventFilter(self, widget, event)          
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if event.button() == QtCore.Qt.RightButton:
                #close full screen win
                self.fullScreenLabel.close()
            if event.button() == QtCore.Qt.LeftButton:
                self.leftMousePress = True
                self.origin = event.pos()
                if not self.rubberBand:
                    self.rubberBand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle,self.fullScreenLabel)
                self.rubberBand.setGeometry(QtCore.QRect(self.origin,QtCore.QSize()))
                self.rubberBand.show()
                return True
        if event.type() == QtCore.QEvent.MouseMove:
 
            if self.leftMousePress:
                if self.rubberBand:
                    self.rubberBand.setGeometry(QtCore.QRect(self.origin,event.pos()).normalized())
                return True
        if event.type() == QtCore.QEvent.MouseButtonRelease:
            self.leftMousePress = False
            if self.rubberBand:
                self.termination = event.pos()
                self.rect = QtCore.QRect(self.origin,self.termination)
                self.screenshot = self.fullScreenPixmap.copy(self.rect.x(),self.rect.y(),self.rect.width(),self.rect.height())
                # save
                self.screenshot.save(self.name+'.png', 'png')
                # close
                self.fullScreenLabel.close()
            return True                                
        return False
 
    def createWidgets(self):
        self.fullScreenLabel = QtGui.QLabel()
        self.fullScreenLabel.setCursor(QtCore.Qt.CrossCursor)
 
        self.fullScreenLabel.setAutoFillBackground(True)
 
        self.shotScreenLabel = QtGui.QLabel()
        self.rubberBand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle,self.fullScreenLabel)  
        pal = QtGui.QPalette()
        pal.setBrush(QtGui.QPalette.Highlight,QtGui.QBrush(QtCore.Qt.red))   
        self.rubberBand.setPalette(pal)
 
        self.leftMousePress = False      
 
        self.fullScreenPixmap = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop().winId())
        self.fullScreenLabel.setPixmap(self.fullScreenPixmap)
 
        self.fullScreenLabel.showFullScreen()
 
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    # your file path
    main=Rubb('D:/screenshot01')
    main.fullScreenLabel.show()
    app.exec_()

Comments (0)

› No comments yet.

Leave a Reply

*

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>