版权声明:zhaojanc https://blog.csdn.net/qq_38641985/article/details/81567631
利用QQ或微信自带的截图功能实现区域截图。
在腾讯安装目录下找到PrScrn.dll,并将它放在需要的位置,
将D:/PrScrn.dll修改为你的目录。
如果在maya里面直接使用该代码
import os,subprocess
from PySide2.QtWidgets import QApplication
clipboard = QApplication.clipboard()
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
else:
startupinfo = None
grab = subprocess.Popen('rundll32.exe D:/PrScrn.dll PrScrn', startupinfo=startupinfo)
grab.wait()
dataImage = clipboard.pixmap()
dataImage.save('D:\LenovoC\maya2017\Maya Icon\Grab.png')
如果在maya外部,使用此代码
import os,subprocess,sys
from PySide.QtGui import QApplication
pp = QApplication(sys.argv)
clipboard = QApplication.clipboard()
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
else:
startupinfo = None
grab = subprocess.Popen('rundll32.exe D:/PrScrn.dll PrScrn', startupinfo=startupinfo)
grab.wait()
dataImage = clipboard.pixmap()
dataImage.save('D:/Grab1.jpg')
也可以用SnapShot.exe和CameraDll.dll,在腾讯软件安装目录下找到 SnapShot.exe和CameraDll.dll,并拷贝至同一目录,并将代码拷到一 起,运行即可截图。也可以直接运行exe文件进行截图。
import os,subprocess,sys
from PySide.QtGui import QApplication
pp = QApplication(sys.argv)
clipboard = QApplication.clipboard()
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
else:
startupinfo = None
grab = subprocess.Popen('SnapShot.exe CameraDll.dll CameraDll', startupinfo=startupinfo)
grab.wait()
dataImage = clipboard.pixmap()
dataImage.save('D:/Grab1.jpg')
下面写了一个用pyqt4写的截图工具,如果大家不想要使用腾讯的东西,可以研究一下。
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,name=None,parent=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
print self.name
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.blue))
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)
main=Rubb(r'C:\Users\Administrator\Desktop\mayaApi\screen')
main.fullScreenLabel.show()
app.exec_()
最后,送上一个可以在图片上截图的小代码,今天就到这里了。
#-*- coding:utf-8 -*-
import cv2
import os,sys
global img
global point1, point2
reload(sys)
sys.setdefaultencoding("utf-8")
def on_mouse(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN:
point1 = (x,y)
cv2.circle(img2, point1, 10, (0,255,0), 5)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): #按住左键拖曳
cv2.rectangle(img2, point1, (x,y), (255,0,0), 5)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP:
point2 = (x,y)
cv2.rectangle(img2, point1, point2, (0,0,255), 5)
cv2.imshow('image', img2)
min_x = min(point1[0],point2[0])
min_y = min(point1[1],point2[1])
width = abs(point1[0] - point2[0])
height = abs(point1[1] -point2[1])
cut_img = img[min_y:min_y+height, min_x:min_x+width]
name='myTu_'
path= os.path.abspath(os.path.join(os.getcwd()))
#path=path.decode('utf-8')
files =os.listdir(path)
t=0
for i in files:
if "myTu" in i:
t=t+1
cv2.imwrite(name+str(t)+".jpg", cut_img)
def main():
global img
img = cv2.imread('1.jpg')
cv2.namedWindow('image')
cv2.setMouseCallback('image', on_mouse)
cv2.imshow('image', img)
cv2.waitKey(0)
if __name__ == '__main__':
main()