def moveFromTo(p1, p2): if len(win32api.EnumDisplayMonitors(None, None)) > 1: (hMon, hDC, (left, top, right, bottom)) = win32api.EnumDisplayMonitors(None, None)[1] if left < 0: p1 = (p1[0] + left, p1[1]) p2 = (p2[0] + left, p2[1]) try: # slope of our line m = (p2[1] - p1[1]) / (p2[0] - p1[0]) # y intercept of our line i = p1[1] - m * p1[0] # current point cP = list(p1) # while loop comparison comp = isGreater # moving left to right or right to left inc = -1 # switch for moving to right if (p2[0] > p1[0]): comp = isLess inc = 1 # move cursor one pixel at a time while comp(cP[0], p2[0]): win32api.SetCursorPos(cP) cP[0] += inc # get next point on line cP[1] = m * cP[0] + i # slow it down time.sleep(0.01) except: pass
def window_capture(): hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #size of pic saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) cc = time.gmtime() #print cc bmpname = str(cc[0]) + "-" + str(cc[1]) + "-" + str(cc[2]) + "-" + str( cc[3]) + "-" + str(cc[4]) + "-" + str(cc[5]) + '.bmp' saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname[:-4] + ".jpg") os.remove(bmpname) jpgname = bmpname[:-4] + '.jpg' #djpgname=dpath+jpgname #copy_command = "move %s %s" % (jpgname, djpgname) #os.popen(copy_command) #return bmpname[:-4]+'.jpg' return bmpname[:-4]
def screen_image(self, begin, dpath, image_name, weight, height, stop): # 截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录 # 返回图片文件名,文件名格式name.png hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((begin), (weight, height), mfcDC, (stop), win32con.SRCCOPY) # print(w,h) # cc=time.gmtime() # bmpname=str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp' bmpname = image_name saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname + ".png") os.remove(bmpname) jpgname = bmpname + '.png' djpgname = dpath + r"/" + jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen(copy_command) return bmpname + '.png'
def grab(filename, left, top, right, bottom): hwnd = 0 # 窗口的编号 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] right = min(w, right) bottom = min(h, bottom) # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, right - left, bottom - top) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (right - left, bottom - top), mfcDC, (left, top), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename)
def window_capture(): hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, os.path.join(SCREENIMG, 'screen.png')) # 销毁占用的内存 win32gui.DeleteObject(saveBitMap.GetHandle()) saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(0, hwndDC) time.sleep(0.2)
def __init__(self): super().__init__() self.availableMonitors = [] monitors = win32api.EnumDisplayMonitors() for monitor in monitors: # monitor[2] => (0, 0, 1920, 1080) [ x start, y start, x end, y end ] # get the resolution so we can find the correct position X = monitor[2][2] - monitor[2][0] Y = monitor[2][3] - monitor[2][1] resolution = str(X) + "x" + str(Y) # offsets to add to button position to put it on the right monitor xOffset = monitor[2][0] yOffset = monitor[2][1] self.availableMonitors.append({ 'resolution': resolution, 'xOffset': xOffset, 'yOffset': yOffset }) self.placeWindow(0) self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint) self.show()
def window_capture(dpath,imagetype = '.jpg'): ''''' @note: 截屏函数,调用方法window_capture('E:\\') ,参数为指定保存的目录 返回图片文件名,文件名格式:ttt.jpg ''' hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC=win32ui.CreateDCFromHandle(hwndDC) saveDC=mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev=win32api.EnumDisplayMonitors(None,None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #图片大小 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY) # cc=time.gmtime() # bmpname=str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp' bmpname = 'ttt' saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname+imagetype) os.remove(bmpname) jpgname=bmpname+imagetype djpgname=dpath+jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen(copy_command) return bmpname+imagetype
def window_capture(filename, hwnd): # hwnd = win32gui.GetForegroundWindow() # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) rect = win32gui.GetWindowRect(hwnd) w = rect[2] - rect[0] h = rect[3] - rect[1] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename)
def getMonitorCoordinates(targetMonitor): ''' Enumerates the available monitor. Return the Screen Dimensions of the selected monitor. ''' HMONITOR = 0 HDCMONITOR = 1 SCREENRECT = 2 try: monitors = win32api.EnumDisplayMonitors(None, None) if targetMonitor > len(monitors) - 1: raise MonitorSelectionOutOfBounds( "Monitor argument exceeds attached number of devices.\n" "There are only %d display devices attached.\n" % len(monitors) + "Please select appropriate device ( 0=Primary, 1=Secondary, etc..)." ) left, top, right, bottom = monitors[targetMonitor][SCREENRECT] width = right - left height = bottom finally: # I can't figure out what to do with the handle to the Monitor # that gets returned from EnumDisplayMonitors (the first object in # the tuple). Trying to close it throws an error.. Does it not need # cleaned up at all? Most of the winApi is back magic to me... # These device context handles were the only things that I could Close() for monitor in monitors: monitor[HDCMONITOR].Close() return (left, top, width, height)
def window_capture(filename, position): hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((position[0], position[1]), (position[2], position[3]), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) # # beg = time.time() # for i in range(1): # window_capture("haha.jpg") # end = time.time() # print(end - beg)
def getMoniterResolution(MoniterDevNum): #MoniterDevNum是显示器编号 MoniterDev = win32api.EnumDisplayMonitors(None, None) #print(MoniterDev) #打印显示器信息 MoniterWidth = MoniterDev[MoniterDevNum][2][2] #显示器分辨率的宽度 MoniterHigh = MoniterDev[MoniterDevNum][2][3] #显示器分辨率的高度 #print (MoniterWidth,MoniterHigh) return MoniterWidth, MoniterHigh
def click_capture(dir, namePrefix, x, y): hee = sys.getwindowsversion()[0] filename = dir if not os.path.exists(filename): os.mkdir(filename) filename = filename + "\\" + namePrefix filename = filename + '.png' hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] if hee == 10: w = int(1.25 * w) h = int(1.25 * h) # 图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) return filename
def window_capture(dir, namePrefix, left, top, right, down): filename = dir if not os.path.exists(filename): os.mkdir(filename) filename = filename + "\\" + namePrefix filename = filename + '.png' hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) saveDC.BitBlt((left, top), (right - left, down - top), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) return filename
def get_window_taskbar_size(): resW, resH = get_screen_resolution() monitors = win32api.EnumDisplayMonitors() display1 = win32api.GetMonitorInfo(monitors[0][0]) tbH = resH - display1['Work'][3] tbW = resW return tbW, tbH
def win32_cap(filename=None, rect=None): if filename == None or filename[-4:] != '.png': i = random.randrange(1, 1000) file_dir = filename if filename else 'temp_image' os.makedirs(file_dir, exist_ok=True) filename = join(file_dir, str(i) + '.png') else: file_dir = os.path.dirname(filename) os.makedirs(file_dir, exist_ok=True) MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] x, y = 0, 0 if rect is not None: y, x, h, w = rect hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(x0, y0)长宽为(w, h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (x, y), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) im = cv2.imread(filename) return im
def getno(hwnd): #返回参数所指定的窗口的设备环境 hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取设备描述表 mfcDC = win32ui.CreateDCFromHandle(hwndDC) # 通过设备描述表创建创建一个与应用程序的当前显示器兼容的内存设备上下文环境,如果成功,则返回内存设备上下文环境的句柄 saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取桌面分辨率 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] ck = win32gui.GetClientRect(hwnd) ck2 = win32gui.GetWindowRect(hwnd) w = ck[2] h = ck[3] b_h = ck2[3] - ck2[1] - h print(w, h) # 图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, b_h), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, "截图.png")
def screenshot(): hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) # win32gui.DeleteObject(saveBitMap.GetHandle()) # saveDC.DeleteDC() # mfcDC.DeleteDC() # win32gui.ReleaseDC(hwnd, hwndDC) return im
def window_capture(filename): aa, bb = win32api.GetCursorPos() #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0) #win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEUP,0,0) #win32api.SetCursorPos((0,0)) #sleep(0.005) hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) hh = True while hh: try: saveBitMap.SaveBitmapFile(saveDC, filename) hh = False except: sleep(0.05)
def get_workareas(): try: workareas = [] for m in win32api.EnumDisplayMonitors(None, None): mi = win32api.GetMonitorInfo(m[0]) screenlog("get_workareas() GetMonitorInfo(%s)=%s", m[0], mi) #absolute workarea / monitor coordinates: wx1, wy1, wx2, wy2 = mi['Work'] mx1, my1, mx2, my2 = mi['Monitor'] assert mx1 < mx2 and my1 < my2, "invalid monitor coordinates" #clamp to monitor, and make it all relative to monitor: rx1 = max(0, min(mx2 - mx1, wx1 - mx1)) ry1 = max(0, min(my2 - my1, wy1 - my1)) rx2 = max(0, min(mx2 - mx1, wx2 - mx1)) ry2 = max(0, min(my2 - my1, wy2 - my1)) assert rx1 < rx2 and ry1 < ry2, "invalid relative workarea coordinates" geom = rx1, ry1, rx2 - rx1, ry2 - ry1 #GTK will return the PRIMARY monitor first, #so we have to do the same thing: if mi['Flags'] & MONITORINFOF_PRIMARY: workareas.insert(0, geom) else: workareas.append(geom) assert len(workareas) > 0 screenlog("get_workareas()=%s", workareas) return workareas except Exception as e: screenlog.warn("failed to query workareas: %s", e) return []
def window_capture(num): filename = Tools.SCREEN_PATH if (num == 1): time.sleep(5) hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) # print(MoniterDev[1]) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print(w,h) #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename)
def get_monitor_locations2(): """Get the location of each monitor. Returns: ((x1, y1, x2, y2),) as 4 integers for each monitor """ return tuple(m[2] for m in win32api.EnumDisplayMonitors())
def window_capture(self, filename, args): """filename:文件名,args是[x,y,w,h]列表""" hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) # w = MoniterDev[0][2][2] # h = MoniterDev[0][2][3] x = args[0] #截图区域左上角的x,y坐标 y = args[1] w = args[2] #截图区域的宽和高 h = args[3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (x,y), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename)
def window_capture(self, filename=''): hwnd = self.hwnd # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #设置所需监控器大小(调节分辨率需修改) w = 1280 h = 720 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) #创建opencv标准图像格式,作为返回值 signedIntsArray = saveBitMap.GetBitmapBits(True) im_opencv = np.frombuffer(signedIntsArray, dtype='uint8') print(im_opencv) im_opencv.shape = (h, w, 4) print(im_opencv) print(type(im_opencv)) cv2.cvtColor(im_opencv, cv2.COLOR_BGRA2RGB) cv2.imwrite('CV2.png', im_opencv) return im_opencv
def window_capture(): try: hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (820, 650), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, "D:\\dh2\\system\\0.PNG") except: pass
def window_capture(filename): hwnd = 0 # 視窗的編號,0號表示當前活躍視窗 # 根據視窗控制代碼獲取視窗的裝置上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根據視窗的DC獲取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC建立可相容的DC saveDC = mfcDC.CreateCompatibleDC() # 建立bigmap準備儲存圖片 saveBitMap = win32ui.CreateBitmap() # 獲取監控器資訊 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #圖片大小 # 為bitmap開闢空間 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,將截圖儲存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 擷取從左上角(0,0)長寬為(w,h)的圖片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename) beg = time.time() for i in range(10): window_capture("haha.jpg") end = time.time() print(end - beg)
def window_capture(hwnd,filename): #hwnd = 0x00091A0E # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)00091A0E hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] w= 1035 h=750 # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) filename = os.getcwd() +"//pic//"+ filename saveBitMap.SaveBitmapFile(saveDC, filename)
def window_capture(dpath): ''''' 截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录 返回图片文件名,文件名格式:日期.jpg 如:2009328224853.jpg ''' hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC=win32ui.CreateDCFromHandle(hwndDC) saveDC=mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev=win32api.EnumDisplayMonitors(None,None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #图片大小 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY) time_temp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) a1 = str(time_temp)[0:4] a2 = str(time_temp)[5:7] a3 = str(time_temp)[8:10] a4 = str(time_temp)[11:13] a5 = str(time_temp)[14:16] a6 = str(time_temp)[17:19] now_time = (a1+a2+a3+a4+a5+a6) bmpname = now_time+'.bmp' saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname[:-4]+".jpg") os.remove(bmpname) jpgname=bmpname[:-4]+'.jpg' djpgname=dpath+jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen(copy_command) return bmpname[:-4]+'.jpg'
def script_update(settings): global g g.settings = settings global slide_scene slide_scene = obs.obs_data_get_string(settings, "slide_scene") global monitors monitors = [] for hMonitor, hdcMonitor, pyRect in win32api.EnumDisplayMonitors(): monitors.append(Monitor(hMonitor, hdcMonitor, pyRect, win32api.GetMonitorInfo(hMonitor)["Device"])) global monitor monitor = obs.obs_data_get_int(settings, "monitor") global screen_sourcename screen_sourcename = obs.obs_data_get_string(settings, "screen_sourcename") global slide_visible_duration slide_visible_duration = obs.obs_data_get_int(settings, "slide_visible_duration") global fadeout_duration fadeout_duration = obs.obs_data_get_double(settings, "fadeout_duration") global refresh_interval refresh_interval = obs.obs_data_get_double(settings, "refresh_interval") global camera_sourcename camera_sourcename = obs.obs_data_get_string(settings, "camera_sourcename")
def window_capture(self, filename): """ 截图 :param filename: :return: """ # hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(self.handle) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从左上角(0,0)长宽为(w,h)的图片 saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, filename)
def window_capture(self): ''''' 截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录 返回图片文件名,文件名格式:日期.jpg 如:2009328224853.jpg ''' c = control() hwnd = 0 dpath = c.GetVol() + ':\\DLP\\Result\\' hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #图片大小 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY) cc = time.gmtime() bmpname = str(cc[0]) + str(cc[1]) + str(cc[2]) + str(cc[3] + 8) + str( cc[4]) + str(cc[5]) + '_WindowsCapture' + '.bmp' saveBitMap.SaveBitmapFile(saveDC, bmpname) Image.open(bmpname).save(bmpname[:-4] + ".jpg") os.remove(bmpname) jpgname = bmpname[:-4] + '.jpg' djpgname = dpath + jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen(copy_command) return True