Exemple #1
0
def get_CG_imagewrapper(rect=None):
    from xpra.codecs.image_wrapper import ImageWrapper
    assert CG, "cannot capture without Quartz.CoreGraphics"
    if rect is None:
        x = 0
        y = 0
        region = CG.CGRectInfinite
    else:
        x, y, w, h = rect
        region = CG.CGRectMake(x, y, roundup(w, 2), roundup(h, 2))
    image = CG.CGWindowListCreateImage(
        region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID,
        CG.kCGWindowImageNominalResolution)  #CG.kCGWindowImageDefault)
    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bpc = CG.CGImageGetBitsPerComponent(image)
    bpp = CG.CGImageGetBitsPerPixel(image)
    rowstride = CG.CGImageGetBytesPerRow(image)
    alpha = CG.CGImageGetAlphaInfo(image)
    alpha_str = ALPHA.get(alpha, alpha)
    log(
        "get_CG_imagewrapper(..) image size: %sx%s, bpc=%s, bpp=%s, rowstride=%s, alpha=%s",
        width, height, bpc, bpp, rowstride, alpha_str)
    prov = CG.CGImageGetDataProvider(image)
    argb = CG.CGDataProviderCopyData(prov)
    return ImageWrapper(x, y, width, height, argb, "BGRX", 24, rowstride)
def get_window_image(window_d, crop_title=True):
    """
    Gets an image object of the contents of the given window. Only valid in macOS.
    See: https://stackoverflow.com/a/53607100
    See: https://stackoverflow.com/a/22967912
    :param int window_d: the id of the window that we want to capture.
    :param bool crop_title: whether to crop the title bar part of the window.
    :rtype: PIL.Image.Image
    :return: the image representation of the given window.
    """
    # check for mac OS
    if platform.system() != 'Darwin':
        raise ValueError('Not supported in non-macOS platforms!')

    # get CG image
    cg_img = CG.CGWindowListCreateImage(
        CG.CGRectNull,
        CG.kCGWindowListOptionIncludingWindow,
        window_d,
        CG.kCGWindowImageBoundsIgnoreFraming | CG.kCGWindowImageBestResolution)

    width = CG.CGImageGetWidth(cg_img)
    height = CG.CGImageGetHeight(cg_img)
    pixel_data = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(cg_img))
    bpr = CG.CGImageGetBytesPerRow(cg_img)

    # create image and crop title
    img = Image.frombuffer('RGBA', (width, height), pixel_data, 'raw', 'BGRA', bpr, 1)
    if crop_title:
        img = img.crop((0, TITLE_BAR_HEIGHT, width, height))
    return img
Exemple #3
0
def screen_record_v3():
    last_time = time.time()
    while 1:
        region = CG.CGRectMake(100, 100, 400, 400)
        image = CG.CGWindowListCreateImage(region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageDefault)
        width = CG.CGImageGetWidth(image)
        height = CG.CGImageGetHeight(image)
        prov = CG.CGImageGetDataProvider(image)
        data = CG.CGDataProviderCopyData(prov)
        byteperrow = CG.CGImageGetBytesPerRow(image)
        #print prov
        #<CGDataProvider 0x7fc19b1022f0>
        #print type(data)
        #<objective-c class __NSCFData at 0x7fff78073cf8>
        img = np.frombuffer(data, dtype=np.uint8)
        img = img.reshape((height, byteperrow//4,4))
        img = img[:, :width, :]
        print('loop took {0} seconds' . format(time.time()-last_time))
        img = process_img(img)
        # the above take roughly 0.01s
        last_time = time.time()
        cv2.imshow('window',img)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
Exemple #4
0
    def capture(self) -> np.array:
        """ Capture border-less window content and return it as a raw pixel array """
        screenBounds = CG.CGRectNull
        listOption = CG.kCGWindowListOptionIncludingWindow
        windowID = self.window['kCGWindowNumber']
        imageOption = CG.kCGWindowImageBoundsIgnoreFraming | CG.kCGWindowImageShouldBeOpaque
        screenshot = CG.CGWindowListCreateImage(screenBounds, listOption,
                                                windowID, imageOption)

        width, height = CG.CGImageGetWidth(screenshot), CG.CGImageGetHeight(
            screenshot)
        bytesPerRow = CG.CGImageGetBytesPerRow(screenshot)

        if width <= 1 and height <= 1:
            warnings.warn(
                "Can't capture window because the process is minimized",
                RuntimeWarning)
            return None

        dataProvider = CG.CGImageGetDataProvider(screenshot)
        rawPixels = CG.CGDataProviderCopyData(dataProvider)
        image = np.frombuffer(rawPixels, dtype=np.uint8).reshape(
            [height, bytesPerRow // 4, 4])

        return image[self.TitleBarHeight:height, 0:width, 0:3]
def scoreGrab(dy):
    """Captures an image of the scoreboard and converts it to RGB array.
    Returns scoreboard readings from ScoreReading.py scoreBoard() method."""
    region = CG.CGRectMake(434, 142 + dy, 190, 88)
    image = CG.CGWindowListCreateImage(region,
                                       CG.kCGWindowListOptionOnScreenOnly,
                                       CG.kCGNullWindowID,
                                       CG.kCGWindowImageDefault)
    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)
    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))

    array = np.frombuffer(pixeldata, dtype=np.uint8)
    array = array.reshape((height, bytesperrow // 4, 4))
    array = array[:, :, [2, 1, 0]][:, :width, :]

    r, g, b = array.T

    grey = (r == 196) & (g == 196) & (b == 196)
    array[:, :, :3][grey.T] = (0, 0, 0)

    other = (r > 0) & (g >= 0) & (b > 0)
    array[:, :, :3][other.T] = (255, 255, 255)

    return array, sr.scoreBoard(array)
    def update(self, _):
        region = CG.CGRectInfinite

        # Create screenshot as CGImage
        t = time.time()

        image = CG.CGWindowListCreateImage(region,
                                           CG.kCGWindowListOptionOnScreenOnly,
                                           CG.kCGNullWindowID,
                                           CG.kCGWindowImageDefault)

        bytesperrow = CG.CGImageGetBytesPerRow(image)

        pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
        image = numpy.frombuffer(pixeldata, dtype=numpy.uint8)
        image = image.reshape((2400, bytesperrow // 4, 4))
        # image = image[:, :width, :]
        im_rgb = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
        image = cv2.flip(im_rgb, 0)
        image = cv2.resize(image, (1920, 1200))

        # pilimage = Image.fromarray(image)
        # print(pilimage.size)
        # cv2.imwrite("CG.png",image)
        fgmask = self.fgbg.apply(image)
        #
        dst = cv2.bitwise_or(image, image, mask=fgmask)
        # image = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
        # imgdata=numpy.fromstring(_data,dtype=numpy.uint8).tobytes()
        # image = image.resize((int(image.width / 2), int(image.height / 2)))
        self.texture.blit_buffer(dst.tobytes(), colorfmt='rgb')
        with self.widget.canvas:
            Rectangle(texture=self.texture, pos=(0, 0), size=Window.size)
        print(time.time() - t)
Exemple #7
0
def screen_record():
    region = CG.CGRectMake(100, 122, 400, 378)
    #image = CG.CGWindowListCreateImage(region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageDefault)
    image = CG.CGWindowListCreateImage(region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageNominalResolution)
    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    prov = CG.CGImageGetDataProvider(image)
    data = CG.CGDataProviderCopyData(prov)
    byteperrow = CG.CGImageGetBytesPerRow(image)
    img = np.frombuffer(data, dtype=np.uint8)
    img = img.reshape((height, byteperrow//4,4))
    img = img[:, :width, :]
    return img
Exemple #8
0
def get_img(x, y, w, h):
    region = CG.CGRectMake(x, y, w, h)
    image = CG.CGWindowListCreateImage(region, 1, 0, 0)

    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)

    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
    image = np.frombuffer(pixeldata, dtype=np.uint8)
    image = image.reshape((height, bytesperrow // 4, 4))
    im = image[:, :width, :]
    return im
Exemple #9
0
    def img_grab(self):
        img = CG.CGWindowListCreateImage(self.roi,
                                         CG.kCGWindowListOptionOnScreenOnly,
                                         CG.kCGNullWindowID,
                                         CG.kCGWindowImageDefault)

        pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(img))
        width = CG.CGImageGetWidth(img)
        height = CG.CGImageGetHeight(img)
        bytesperrow = CG.CGImageGetBytesPerRow(img)
        img_mat = np.frombuffer(pixeldata, dtype=np.uint8).reshape(
            (height, bytesperrow // 4, 4))

        return img_mat[:, :width, :]
Exemple #10
0
 def screen_record(self):
     image = CG.CGWindowListCreateImage(self.region,
                                        CG.kCGWindowListOptionOnScreenOnly,
                                        CG.kCGNullWindowID,
                                        CG.kCGWindowImageDefault)
     width = CG.CGImageGetWidth(image)
     height = CG.CGImageGetHeight(image)
     prov = CG.CGImageGetDataProvider(image)
     data = CG.CGDataProviderCopyData(prov)
     byteperrow = CG.CGImageGetBytesPerRow(image)
     img = np.frombuffer(data, dtype=np.uint8)
     img = img.reshape((height, byteperrow // 4, 4))
     img = img[:, :width, :]
     return img
Exemple #11
0
 def get_pixel_data(region: Optional[Region] = None):
     region = (CoreGraphics.CGRectInfinite if region is None else
               CoreGraphics.CGRectMake(region.x1, region.y1, region.x2 -
                                       region.x1, region.y2 - region.y1))
     image = CoreGraphics.CGWindowListCreateImage(
         region,
         CoreGraphics.kCGWindowListOptionOnScreenOnly,
         CoreGraphics.kCGNullWindowID,
         CoreGraphics.kCGWindowImageDefault,
     )
     pixel_data = CoreGraphics.CGDataProviderCopyData(
         CoreGraphics.CGImageGetDataProvider(image))
     bytes_per_row = CoreGraphics.CGImageGetBytesPerRow(image) // 4
     return pixel_data, bytes_per_row
def observationGrab():
    """Captures image of the game screen and converts it to RGB array."""
    region = CG.CGRectMake(401, 130, 634, 445)
    image = CG.CGWindowListCreateImage(region,
                                       CG.kCGWindowListOptionOnScreenOnly,
                                       CG.kCGNullWindowID,
                                       CG.kCGWindowImageDefault)
    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)
    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
    array = np.frombuffer(pixeldata, dtype=np.uint8)
    array = array.reshape((height, bytesperrow // 4, 4))
    array = array[:, :, [2, 1, 0]][:, :width, :]
    return array
Exemple #13
0
def screenshot(x, y, w, h):
    region = CG.CGRectMake(x, y, w, h)

    # Create screenshot as CGImage
    image = CG.CGWindowListCreateImage(region,
                                       CG.kCGWindowListOptionOnScreenOnly,
                                       CG.kCGNullWindowID,
                                       CG.kCGWindowImageDefault)

    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)

    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
    image = np.frombuffer(pixeldata, dtype=np.uint8)
    image = image.reshape((height, bytesperrow // 4, 4))
    return image[:, :width, :]
Exemple #14
0
def capture_window(window_info):
    image = CG.CGWindowListCreateImage(
        CG.CGRectNull,
        CG.CG.kCGWindowListOptionIncludingWindow,
        window_info['kCGWindowNumber'],
        CG.kCGWindowImageBoundsIgnoreFraming | CG.kCGWindowImageBestResolution)
        # CG.kCGWindowImageBoundsIgnoreFraming | CG.kCGWindowImageNominalResolution)


    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)

    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
    image = np.frombuffer(pixeldata, dtype=np.uint8)
    image = image.reshape((height, bytesperrow // 4, 4))
    image = image[:, :width, :]

    return image
Exemple #15
0
 def get_image(self, x, y, width, height, logger=None):
     #region = CG.CGRectMake(0, 0, 100, 100)
     region = CG.CGRectInfinite
     image = CG.CGWindowListCreateImage(region,
                 CG.kCGWindowListOptionOnScreenOnly,
                 CG.kCGNullWindowID,
                 CG.kCGWindowImageDefault)
     width = CG.CGImageGetWidth(image)
     height = CG.CGImageGetHeight(image)
     bpc = CG.CGImageGetBitsPerComponent(image)
     bpp = CG.CGImageGetBitsPerPixel(image)
     rowstride = CG.CGImageGetBytesPerRow(image)
     alpha = CG.CGImageGetAlphaInfo(image)
     alpha_str = ALPHA.get(alpha, alpha)
     if logger:
         logger("OSXRootWindowModel.get_image(..) image size: %sx%s, bpc=%s, bpp=%s, rowstride=%s, alpha=%s", width, height, bpc, bpp, rowstride, alpha_str)
     prov = CG.CGImageGetDataProvider(image)
     argb = CG.CGDataProviderCopyData(prov)
     return ImageWrapper(0, 0, width, height, argb, "BGRX", 24, rowstride)
Exemple #16
0
def get_screenshot():
    # # im=ImageGrab.grab(bbox=(28, 46, 640, 640)) # X1,Y1,X2,Y2
 


    # # while 1:
    # sct.get_pixels(mon)
    # img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    #     # cv2.imshow('test', np.array(img))
    #     # if cv2.waitKey(25) & 0xFF == ord('q'):
    #     #     cv2.destroyAllWindows()
    #     # break
    
    # answer = np.array(img)
    # # del sct, img, mon

    # printscreen_pil =  ImageGrab.grab(bbox=(46,28,640,640))
    # # time_stamp1 = time.time()
    # printscreen_numpy =   np.array(printscreen_pil, dtype='uint8').reshape((printscreen_pil.size[1],printscreen_pil.size[0],4))
    # region = CG.CGRectMake(28, 46, 640, 640)

    # Create screenshot as CGImage
    image = CG.CGWindowListCreateImage(
        region,
        CG.kCGWindowListOptionOnScreenOnly,
        CG.kCGNullWindowID,
        CG.kCGWindowImageDefault)

    width = CG.CGImageGetWidth(image)
    height = CG.CGImageGetHeight(image)
    bytesperrow = CG.CGImageGetBytesPerRow(image)

    pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image))
    image = np.frombuffer(pixeldata, dtype=np.uint8)
    image = image.reshape((height, bytesperrow//4, 4))
    image = image[:,:width,:]
    return  image