Exemple #1
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 #2
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]
Exemple #3
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)
    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
Exemple #5
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)
Exemple #6
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)
Exemple #7
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)
Exemple #8
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)
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)
Exemple #10
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
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
    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 #13
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]
Exemple #14
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)
Exemple #15
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 #16
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
    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)
Exemple #18
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
Exemple #19
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
Exemple #20
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 #21
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, :]
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 #23
0
 def capture(self, region=None):
     if region == None:
         region = CG.CGRectInfinite
     else:
         region = CG.CGRectMake(region)
     image = CG.CGWindowListCreateImage(region,
                                        CG.kCGWindowListOptionOnScreenOnly,
                                        CG.kCGNullWindowID,
                                        CG.kCGWindowImageDefault)
     provider = CG.CGImageGetDataProvider(image)
     self._data = CG.CGDataProviderCopyData(provider)
     self.width = CG.CGImageGetWidth(image)
     self.height = CG.CGImageGetHeight(image)
     imgdata = np.fromstring(self._data, dtype=np.uint8).reshape(
         len(self._data) / 4, 4)
     return imgdata[:self.width * self.height, :-1].reshape(
         self.height, self.width, 3)
Exemple #24
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 #25
0
    def capture(self):
        region = CG.CGRectMake(x, y, 1, 1)

        # 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)
    def capture(self, region=None):
        global xxxx

        if region is None:
            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)
        xxxx = self._data
Exemple #27
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 #28
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 #29
0
    def capture(self, region=None):
        if region is None:
            region = CG.CGRectInfinite
        else:
            if region.size.width % 2 > 0:
                emsg = "Capture region width should be even (was %s)" % (
                    region.size.width)
                raise ValueError(emsg)

        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)
Exemple #30
0
def average_color_from_screenshot(path=None, path2=None, region=None):
    if region is None:
        region = CG.CGRectInfinite

    # Create screenshot as CGImage
    image = CG.CGDisplayCreateImage(get_widest_display())

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

    if path:
        drawImageToFile(image, path)

    # Create smaller image2 from captured image
    width2 = 1
    height2 = 1
    context = CG.CGBitmapContextCreate(None, width2, height2, 8, width2 * 4,
                                       CG.CGColorSpaceCreateDeviceRGB(), 1)

    CG.CGContextScaleCTM(context,
                         float(width2) / width,
                         float(height2) / height)
    CG.CGContextSetInterpolationQuality(context, CG.kCGInterpolationHigh)

    CG.CGContextDrawImage(context, NSMakeRect(0, 0, width, height), image)
    image2 = CG.CGBitmapContextCreateImage(context)

    if path2:
        drawImageToFile(image2, path2)

    # Extract pixel value
    prov2 = CG.CGImageGetDataProvider(image2)
    data2 = CG.CGDataProviderCopyData(prov2)
    c1 = struct.unpack_from("BBBB", data2, offset=0)

    c2 = NSColor.colorWithCalibratedRed_green_blue_alpha_(
        c1[0] / 255.0, c1[1] / 255.0, c1[2] / 255.0, c1[3] / 255.0)
    result = (c2.hueComponent(), c2.saturationComponent(),
              c2.brightnessComponent())

    return result