Example #1
0
def capture(image_directory=directory):
    # 图片存储
    if not os.path.exists(image_directory):
        os.makedirs(image_directory)
    filename = str(int(time.time())) + '.jpg'
    image_path = os.path.join(image_directory, filename)
    # 获取最前端窗口
    hwnd = win32gui.GetForegroundWindow()
    title = win32gui.GetWindowText(hwnd)
    # 从配置中获取显示器分辨率
    width = config.SCREEN_WIDTH
    height = config.SCREEN_HEIGHT
    # 获取桌面
    desktop_hwnd = win32gui.GetDesktopWindow()
    desktop_dc = win32gui.GetWindowDC(desktop_hwnd)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)
    # 创建一个内存设备描述表
    mem_dc = img_dc.CreateCompatibleDC()
    save_bitmap = win32ui.CreateBitmap()
    save_bitmap.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(save_bitmap)
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (0, 0), win32con.SRCCOPY)
    # 保存tup
    save_bitmap.SaveBitmapFile(mem_dc, image_path)
    # 释放内存
    mem_dc.DeleteDC()
    win32gui.DeleteObject(save_bitmap.GetHandle())
    # 打印最前端窗口
    return title
Example #2
0
def get_desk():
    # 获取桌面
    hdesktop = win32gui.GetDesktopWindow()

    # 分辨率适配
    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    # 创建设备描述表
    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)

    # 创建一个内存设备描述表
    mem_dc = img_dc.CreateCompatibleDC()
    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)  # 为bitmap开辟空间
    mem_dc.SelectObject(screenshot)  # 将截图保存到Bitmap中
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)  # 截取从左上角(0,0)长宽为(w,h)的图片

    # 保存到文件
    time_tup = time.localtime(time.time())
    format_time = "%Y-%m-%d_%a_%H-%M-%S"
    cur_time = time.strftime(format_time, time_tup)
    screenshot.SaveBitmapFile(mem_dc, '{}.jpg'.format(cur_time))

    # 释放内存
    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())
Example #3
0
def grab_screen(region=None):
    hwin = win32gui.GetDesktopWindow()

    if region:
        left, top, x2, y2 = region
        width = x2 - left + 1
        height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
Example #4
0
    def shot(cls):
        # grab a handle to the main desktop window
        hdesktop = win32gui.GetDesktopWindow()

        # create a device context
        desktop_dc = win32gui.GetWindowDC(hdesktop)
        img_dc = win32ui.CreateDCFromHandle(desktop_dc)

        # create a memory based device context
        mem_dc = img_dc.CreateCompatibleDC()

        # create a bitmap object
        screenshot = win32ui.CreateBitmap()
        screenshot.CreateCompatibleBitmap(img_dc, cls.width, cls.height)
        mem_dc.SelectObject(screenshot)

        # copy the screen into our memory device context
        mem_dc.BitBlt((0, 0), (cls.width, cls.height), img_dc,
                      (cls.left, cls.top), win32con.SRCCOPY)

        # save the bitmap to a file
        signed_ints_array = screenshot.GetBitmapBits(True)
        screen_img = np.fromstring(signed_ints_array, dtype='uint8')
        screen_img = screen_img.reshape((cls.height, cls.width, 4))
        screen_img = cv2.cvtColor(screen_img, cv2.COLOR_RGBA2RGB)

        # free our objects
        img_dc.DeleteDC()
        mem_dc.DeleteDC()
        win32gui.ReleaseDC(hdesktop, desktop_dc)
        win32gui.DeleteObject(screenshot.GetHandle())
        return screen_img
Example #5
0
def capture_ecran_Win32_V1():
    dernier_temps = time.time()
    while (True):
        hwnd = win32gui.GetDesktopWindow()
        l, t, r, b = win32gui.GetWindowRect(hwnd)
        w = 1280
        h = 720

        hwindc = win32gui.GetWindowDC(hwnd)
        srcdc = win32ui.CreateDCFromHandle(hwindc)
        memdc = srcdc.CreateCompatibleDC()
        bmp = win32ui.CreateBitmap()
        bmp.CreateCompatibleBitmap(srcdc, w, h)
        memdc.SelectObject(bmp)
        memdc.BitBlt((0, 0), (w, h), srcdc, (l, 30), win32con.SRCCOPY)

        signedIntsArray = bmp.GetBitmapBits(False)
        img = np.array(signedIntsArray).astype(dtype="uint8")
        img.shape = (h, w, 4)

        srcdc.DeleteDC()
        memdc.DeleteDC()
        win32gui.ReleaseDC(hwnd, hwindc)
        win32gui.DeleteObject(bmp.GetHandle())

        print(
            f'{time.time()-dernier_temps} seconds - FPS = {1/(time.time()-dernier_temps)}'
        )
        dernier_temps = time.time()
        cv2.imshow('window', cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
Example #6
0
def run_take_screenshot(to):
    print("Taking screenshot...")
    # Taken from Black Hat Python book
    hdesktop = win32gui.GetDesktopWindow()

    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)

    mem_dc = img_dc.CreateCompatibleDC()

    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)

    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)

    screenshot.SaveBitmapFile(mem_dc, "./output-imgs/screenshot.bmp")
    img = Image.open("./output-imgs/screenshot.bmp")
    img.save("./output-imgs/screenshot.png", "png")

    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())

    os.remove("./output-imgs/screenshot.bmp")
    tweet_image("screenshot.png", "Smile!")
Example #7
0
def get_screenshot(filename):
    # grab a handle to the main desktop window
    hdesktop = win32gui.GetDesktopWindow()

    # determine the size of all monitors in pixels
    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    # create a device context
    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)

    # create a memory based device context
    mem_dc = img_dc.CreateCompatibleDC()

    # create a bitmap object
    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)

    # copy the screen into our memory device context
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)

    # save the bitmap to a file
    screenshot.SaveBitmapFile(mem_dc, filename)
    # free our objects
    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())
Example #8
0
def get_screenshot(left, top, width, height):
    import win32ui
    import win32con
    import win32gui
    # 获取桌面截图
    hdesktop = win32gui.GetDesktopWindow()
    # 分辨率适应
    # width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    # height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    # left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    # top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
    # 创建设备描述表
    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)
    # 创建一个内存设备描述表
    mem_dc = img_dc.CreateCompatibleDC()
    # 创建位图对象
    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)
    # 截图至内存设备描述表
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)
    # 将截图保存到文件中
    # screenshot.SaveBitmapFile(mem_dc, 'screenshot.bmp')
    # 转成Image
    bmparray = screenshot.GetBitmapBits(True)
    bmpinfo = screenshot.GetInfo()
    # RGB 是彩色,L 是灰色
    pil_im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                              bmparray, 'raw', 'BGRX', 0, 1).convert("L")
    # 内存释放
    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())
    return pil_im
 def refresh_handle(self):
     old_handle = self.window_handle
     self.window_handle = win32gui.FindWindow(0, self.window_name)
     if self.window_handle:
         # print window_name + ' handle:' + unicode(window_handle)
         # force to show window
         # win32gui.ShowWindow(window_handle, win32con.SW_MAXIMIZE)
         # if self.frame_inv > 100:
         #    # force to focus
         #    win32gui.SetForegroundWindow(self.window_handle)
         #    self.frame_inv = 0
         # self.frame_inv += 1
         w_p = Config.main_config['window']['whole_position']
         # 正常化窗口
         # win32gui.ShowWindow(self.window_handle, 1)
         self.window_rect = win32gui.GetWindowRect(self.window_handle)
         # 固定宽高
         if Config.main_config['flag']['flag_fix_window']:
             win32gui.MoveWindow(self.window_handle, w_p[0], w_p[1], w_p[2],
                                 w_p[3], True)
         else:
             win32gui.MoveWindow(self.window_handle, self.window_rect[0],
                                 self.window_rect[1], w_p[2], w_p[3], True)
     else:
         self.window_handle = win32gui.GetDesktopWindow()
         self.window_rect = win32gui.GetWindowRect(self.window_handle)
     self.window_capture.update(old_handle, self.window_handle)
Example #10
0
def run(**args):

    hdesktop = win32gui.GetDesktopWindow()

    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)

    mem_dc = img_dc.CreateCompatibleDC()

    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)

    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)

    screenshot.SaveBitmapFile(mem_dc, 'C:/WINDOWS/Temp/screenshot.bmp')

    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())

    f = open('C:/WINDOWS/Temp/screenshot.bmp', 'rb')
    r = f.read()
    f.close()
    return str(r)
Example #11
0
def screenshot():
    # 1. 获取桌面像素尺寸
    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    # 2. 创建DC,也就是布置画布
    hdesktop = win32gui.GetDesktopWindow()
    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)
    mem_dc = img_dc.CreateCompatibleDC()

    # 3. 创建位图对象,相当于画笔
    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)

    # 4. 复制屏幕像素,等同于作画
    # 复制一个从左上角坐标(0,0)开始到长宽(width, height)的图片(相当于整个桌面)到我们的mem_dc中,相当于作画
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),
                  win32con.SRCCOPY)

    # 5. 保存图片,释放对象
    file_name = "C:\\Users\\times0ng\\Desktop\\" + time.strftime(
        '%Y_%m_%d_%H_%M_%S') + ".bmp"
    screenshot.SaveBitmapFile(mem_dc, file_name)
    print("保存图片: %s" % file_name)

    # 释放对象
    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())
Example #12
0
def grab_screen(region=(0, 40, 800, 640)):

    hwin = win32gui.GetDesktopWindow()

    left, top, x2, y2 = region
    width = x2 - left + 1
    height = y2 - top + 1

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return img
def get_pixel_colour(i_x, i_y):
    import win32gui
    i_desktop_window_id = win32gui.GetDesktopWindow()
    i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
    long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
    i_colour = int(long_colour)
    return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
def get_screen(x_start=SCREEN_X_START,
               x_end=SCREEN_X_END,
               y_start=SCREEN_Y_START,
               y_end=SCREEN_Y_END):
    hwin = win32gui.GetDesktopWindow()

    width = x_end - x_start
    height = y_end - y_start
    left = x_start
    top = y_start

    if width == 0 or height == 0:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return img
Example #15
0
 def TakeScreenshot(self, file_path):
     width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
     height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
     screen_win = win32gui.GetDesktopWindow()
     win_dc = None
     screen_dc = None
     capture_dc = None
     capture_bitmap = None
     try:
         win_dc = win32gui.GetWindowDC(screen_win)
         screen_dc = win32ui.CreateDCFromHandle(win_dc)
         capture_dc = screen_dc.CreateCompatibleDC()
         capture_bitmap = win32ui.CreateBitmap()
         capture_bitmap.CreateCompatibleBitmap(screen_dc, width, height)
         capture_dc.SelectObject(capture_bitmap)
         capture_dc.BitBlt((0, 0), (width, height), screen_dc, (0, 0),
                           win32con.SRCCOPY)
         pixels = capture_bitmap.GetBitmapBits(True)
         image = Image.frombuffer('RGB', (width, height), pixels, 'raw',
                                  'BGRX', 0, 1)
         image.save(file_path, 'PNG')
         image.close()
     finally:
         if capture_bitmap:
             win32gui.DeleteObject(capture_bitmap.GetHandle())
         if capture_dc:
             capture_dc.DeleteDC()
         if screen_dc:
             screen_dc.DeleteDC()
         if win_dc:
             win32gui.ReleaseDC(screen_win, win_dc)
     return True
Example #16
0
 def get_desktop_color():
     hwnd = win32gui.GetDesktopWindow()
     dc = win32gui.GetWindowDC(hwnd)
     long_colour = win32gui.GetPixel(dc, *ColorCheck.get_mouse_point())
     i_colour = int(long_colour)
     return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16)
                                                          & 0xff)
Example #17
0
def screenshot(filename, x1=None, y1=None, x2=None, y2=None):
    SM_XVIRTUALSCREEN = 76
    SM_YVIRTUALSCREEN = 77
    SM_CXVIRTUALSCREEN = 78
    SM_CYVIRTUALSCREEN = 79
    #
    w = vscreenwidth = win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)
    h = vscreenheigth = win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)
    l = vscreenx = win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)
    t = vscreeny = win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)
    if None not in [x1, y1, x2, y2]:
        w = x2 - x1
        h = y2 - y1
        l = x1
        t = y1
    hwnd = win32gui.GetDesktopWindow()
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (l, t), win32con.SRCCOPY)
    reformat(saveBitMap.GetInfo(), saveBitMap.GetBitmapBits(True), filename)
    mfcDC.DeleteDC()
    saveDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    win32gui.DeleteObject(saveBitMap.GetHandle())
Example #18
0
def FrontFrameCapture():
    # --------------------------------------- Front Frame ----------------------------------------------------------
    # get The Screen window
    Fhwnd = win32gui.GetDesktopWindow()
    FwDC = win32gui.GetWindowDC(Fhwnd)

    # create DC object for
    FdcObj = win32ui.CreateDCFromHandle(FwDC)
    FcDC = FdcObj.CreateCompatibleDC()

    # bitmapping The captured Frame
    FdataBitMap = win32ui.CreateBitmap()
    FdataBitMap.CreateCompatibleBitmap(FdcObj, w, h)

    # Cropping Window
    FcDC.SelectObject(FdataBitMap)
    FcDC.BitBlt((0, 0), (w, h), FdcObj, (0, 30), win32con.SRCCOPY)

    # dataBitMap.SaveBitmapFile(cDC, 'Test.jpg')
    bmpinfo = FdataBitMap.GetInfo()
    bmpstr = FdataBitMap.GetBitmapBits(True)
    im1 = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                           bmpstr, 'raw', 'BGRX', 0, 1)
    Front = np.array(im1)
    Front = cv2.cvtColor(Front, cv2.COLOR_BGR2RGB)
    PressKey(ChangeView)
    time.sleep(delay)
    ReleaseKey(ChangeView)
    # time.sleep(0.0000000000000001)
    # Cleaning Some memory (Avoid Exception)
    FdcObj.DeleteDC()
    FcDC.DeleteDC()
    win32gui.ReleaseDC(Fhwnd, FwDC)
    win32gui.DeleteObject(FdataBitMap.GetHandle())
    return Front
Example #19
0
    def __init__(self, sq):
        dt = win32gui.GetDesktopWindow()
        self.ss = win32gui.GetWindowRect(dt)
        # df = win32gui.GetForegroundWindow()
        # self.sf = win32gui.GetWindowRect(df);

        # self.img_dot = cv2.imread('dot_no_voice.bmp',0);
        # self.h = int(self.img_dot.shape[0]/2);
        self.img_dot = None
        self.h = 22

        self.sq = sq
        self.nlist = {}
        self.count = 0
        self.N = 9
        #the default value is in default can be used in my laptop
        # self.mlist=[[45, 524], [47, 544], [95, 535], [195, 662], [851, 352], [78, 534], [85, 826], [44, 1002]];
        self.mlist = None
        self.start_record = False
        self.start_auto_mouse = False
        self.start_moc_input = False
        self.image_list = []
        self.image_pos = [0, 0, 0, 0]

        self.start_jietu = False

        self.start_hook()
        th2 = threading.Thread(target=self.th_process,
                               args=(),
                               name="capture_yy002")
        th2.start()
Example #20
0
    def get_scaling_factor(self):
        """
        get the dpi scaling factor.

        :rtype: float
        :return: the dpi scaling factor
        """

        hwnd = win32gui.GetDesktopWindow()
        wDC = win32gui.GetWindowDC(hwnd)
        dcObj=win32ui.CreateDCFromHandle(wDC)

        HORZRES = 8
        VERTRES = 10

        DESKTOPHORZRES = 118
        DESKTOPVERTRES = 117

        v_HORZRES = dcObj.GetDeviceCaps( HORZRES)
        v_VERTRES = dcObj.GetDeviceCaps( VERTRES)

        v_DESKTOPHORZRES = dcObj.GetDeviceCaps( DESKTOPHORZRES)
        v_DESKTOPVERTRES = dcObj.GetDeviceCaps( DESKTOPVERTRES)

        dcObj.DeleteDC()

        scaling = round(float(v_DESKTOPVERTRES)/float(v_VERTRES), 2)  #two decimal

        return scaling
Example #21
0
def get_windows_color_depth():
    import win32gui, win32con, win32print
    hwin = win32gui.GetDesktopWindow()
    hwindc = win32gui.GetWindowDC(hwin)
    ans = win32print.GetDeviceCaps(hwindc, win32con.BITSPIXEL)
    win32gui.ReleaseDC(hwin, hwindc)
    return ans
def screenshot(hwnd=None):

    if not hwnd:
        hwnd = win32gui.GetDesktopWindow()

    l, t, r, b = win32gui.GetWindowRect(hwnd[0])
    h = b - t
    w = r - l
    hDC = win32gui.GetWindowDC(hwnd[0])
    myDC = win32ui.CreateDCFromHandle(hDC)
    newDC = myDC.CreateCompatibleDC()

    myBitMap = win32ui.CreateBitmap()
    myBitMap.CreateCompatibleBitmap(myDC, w, h)

    newDC.SelectObject(myBitMap)

    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('#')

    win32gui.SetForegroundWindow(hwnd[0])
    time.sleep(.2)
    newDC.BitBlt((0, 0), (w, h), myDC, (0, 0), win32con.SRCCOPY)
    myBitMap.Paint(newDC)
    myBitMap.SaveBitmapFile(newDC, './screen.bmp')
def grab_screen(x1=0, y1=0, x2=0, y2=0, cvt=cv2.COLOR_BGRA2BGR):
    hwin = win32gui.GetDesktopWindow()

    width = x2 - x1 + 1
    height = y2 - y1 + 1

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (x1, y1), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = numpy.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    # grabs BGRA by default so choose BGRA2<something>
    return cv2.cvtColor(img, cvt)  # cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
Example #24
0
def GetColor(i_x,i_y):
    i_desktop_window_id = win32gui.GetDesktopWindow()
    i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
    long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
    i_colour = int(long_colour)
    print "icolour====",i_colour
    return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
Example #25
0
    def grab_screen(self):
        width = self.width
        height = self.height
        # if self.x_offset <= 0:
        #     width = self.width - self.x_offset + 1
        #     height = self.height - self.y_offset + 1
        # else:

        hwin = win32gui.GetDesktopWindow()
        hwindc = win32gui.GetWindowDC(hwin)
        srcdc = win32ui.CreateDCFromHandle(hwindc)
        memdc = srcdc.CreateCompatibleDC()
        bmp = win32ui.CreateBitmap()
        bmp.CreateCompatibleBitmap(srcdc, width, height)
        memdc.SelectObject(bmp)
        memdc.BitBlt((0, 0), (width, height), srcdc,
                     (self.x_offset, self.y_offset), win32con.SRCCOPY)

        signedIntsArray = bmp.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (height, width, 4)

        srcdc.DeleteDC()
        memdc.DeleteDC()
        win32gui.ReleaseDC(hwin, hwindc)
        win32gui.DeleteObject(bmp.GetHandle())
        self.img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        return self.img
Example #26
0
def shot():
    # 获取桌面
    hdesktop = win32gui.GetDesktopWindow()

    # 分辨率适应
    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    # 创建设备描述表
    desktop_dc = win32gui.GetWindowDC(hdesktop)
    img_dc = win32ui.CreateDCFromHandle(desktop_dc)

    # 创建一个内存设备描述表
    mem_dc = img_dc.CreateCompatibleDC()

    # 创建位图对象
    screenshot = win32ui.CreateBitmap()
    screenshot.CreateCompatibleBitmap(img_dc, width, height)
    mem_dc.SelectObject(screenshot)

    # 截图至内存设备描述表
    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY)

    # 将截图保存到文件中
    screenshot.SaveBitmapFile(mem_dc, 'c:\\WINDOWS\\Temp\\screenshot.bmp')

    # 内存释放
    mem_dc.DeleteDC()
    win32gui.DeleteObject(screenshot.GetHandle())


    
Example #27
0
def ScreenshotByWin32API():
    hwnd = win32gui.GetDesktopWindow()
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    w = win32print.GetDeviceCaps(hwndDC, win32con.DESKTOPHORZRES)
    h = win32print.GetDeviceCaps(hwndDC, win32con.DESKTOPVERTRES)
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
    #add cursor
    curFlags, curH, (curX, curY) = win32gui.GetCursorInfo()
    if curH != 0:
        saveDC.DrawIcon((curX, curY), curH)
    #see https://stackoverflow.com/questions/5999007/active-window-screenshot-with-python-pil-and-windows-api-how-to-deal-with-round
    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)
    im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                          bmpstr, 'raw', 'BGRX', 0, 1)
    #release memory
    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    return im
Example #28
0
 def __init__(self, LoR_hwnd):
     try:
         self.LoR_hwnd = LoR_hwnd
         ## create device context for desktop and client
         self.desktop_hwnd = win32gui.GetDesktopWindow()
         self.desktop_dc = win32gui.GetWindowDC(self.desktop_hwnd)
         self.desktop_img_dc = win32ui.CreateDCFromHandle(self.desktop_dc)
         self.mem_dc = self.desktop_img_dc.CreateCompatibleDC()
         ## default system decorations
         self.sys_wdn_offset = (
             win32api.GetSystemMetrics(win32con.SM_CXFRAME) +
             win32api.GetSystemMetrics(92),  #SM_CXPADDEDBORDER
             win32api.GetSystemMetrics(win32con.SM_CYFRAME) +
             win32api.GetSystemMetrics(92) +
             win32api.GetSystemMetrics(win32con.SM_CYCAPTION))
         self.Lor_app = Region("LoR", self.desktop_img_dc)
         self.update_geometry()
         self.OCR = LoR_OCR.OCR()
         self.matcher = LoR_PatternMatcher.Matcher(self.v_scale)
         print("Error:", win32gui.error())
         pyautogui.FAILSAFE = False
     except win32gui.error:
         print("GUI error in init")
         print(win32gui.error())
         logging.error(win32gui.error)
         logging.error(traceback.format_exc())
         self.mem_dc.DeleteDC()
         self.desktop_img_dc.DeleteDC()
         win32gui.ReleaseDC(self.desktop_hwnd, self.desktop_dc)
Example #29
0
    def __init__(self, window_name=None):
        # find the handle for the window we want to capture.
        # if no window name is given, capture the entire screen
        if window_name is None:
            self.hwnd = win32gui.GetDesktopWindow()
        else:
            self.hwnd = win32gui.FindWindow(None, window_name)
            if not self.hwnd:
                raise Exception('Window not found: {}'.format(window_name))

        # get the window size
        window_rect = win32gui.GetWindowRect(self.hwnd)
        self.w = window_rect[2] - window_rect[0]
        self.h = window_rect[3] - window_rect[1]

        # account for the window border and titlebar and cut them off
        border_pixels = 8
        titlebar_pixels = 30
        self.w = self.w - (border_pixels * 2)
        self.h = self.h - titlebar_pixels - border_pixels
        self.cropped_x = border_pixels
        self.cropped_y = titlebar_pixels

        # set the cropped coordinates offset so we can translate screenshot
        # images into actual screen positions
        self.offset_x = window_rect[0] + self.cropped_x
        self.offset_y = window_rect[1] + self.cropped_y
Example #30
0
 def grab(self, obj):
     print("grabbing screenshot and results")
     print("current scores")
     print(scores[-1])
     import win32gui
     import win32ui
     import win32con
     import win32api
     # grab a handle to the main desktop window
     hdesktop = win32gui.GetDesktopWindow() 
     # determine the size of all monitors in pixels
     width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
     height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
     left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
     top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) 
     # create a device context
     desktop_dc = win32gui.GetWindowDC(hdesktop)
     img_dc = win32ui.CreateDCFromHandle(desktop_dc)
     # create a memory based device context
     mem_dc = img_dc.CreateCompatibleDC() 
     # create a bitmap object
     screenshot = win32ui.CreateBitmap()
     screenshot.CreateCompatibleBitmap(img_dc, width, height)
     mem_dc.SelectObject(screenshot) 
     # copy the screen into our memory device context
     mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),win32con.SRCCOPY) 
     # save the bitmap to a file
     screenshot.SaveBitmapFile(mem_dc, 'C:\\Users\\JCMat\\Desktop\\project\\working_code\\Screenshots.bmp')
     # free our objects
     mem_dc.DeleteDC()
     win32gui.DeleteObject(screenshot.GetHandle())
     plt.plot(scores)
     plt.show()