Beispiel #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
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)
Beispiel #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
Beispiel #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)
Beispiel #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)
Beispiel #7
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)
Beispiel #8
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
Beispiel #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
Beispiel #11
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]
Beispiel #12
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
Beispiel #13
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
Beispiel #14
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)
Beispiel #16
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
Beispiel #17
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
Beispiel #18
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
Beispiel #20
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)
Beispiel #21
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, :]
Beispiel #22
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)
Beispiel #23
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)
Beispiel #24
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
Beispiel #25
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)
Beispiel #26
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
Beispiel #27
0
    def capture(self, region=None):
        """capture screenshot from region
        :region: tuple of (x, y, width, height)
        """

        if region is None:
            region = CG.CGRectInfinite  # full screen

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

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

        # Get width/height of image
        self.width = CG.CGImageGetWidth(image)
        self.height = CG.CGImageGetHeight(image)
Beispiel #28
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
Beispiel #29
0
#!/usr/bin/python
# Gets partial screenshot and saves it into PNG file.
import Quartz.CoreGraphics as cg
region = cg.CGRectMake(
    0, 0, 400, 400)  # You can also use cg.CGRectInfinite for the full screen.
image = cg.CGWindowListCreateImage(region, cg.kCGWindowListOptionOnScreenOnly,
                                   cg.kCGNullWindowID,
                                   cg.kCGWindowImageDefault)
prov = cg.CGImageGetDataProvider(image)
img_data = cg.CGDataProviderCopyData(prov)
img_width, img_height = cg.CGImageGetWidth(image), cg.CGImageGetHeight(image)

from pngcanvas import PNGCanvas
import struct
canvas = PNGCanvas(img_width, img_height)
for x in range(img_width):
    for y in range(img_height):
        # Calculate offset, based on http://www.markj.net/iphone-uiimage-pixel-color/
        offset = 4 * ((img_width * int(round(y))) + int(round(x)))
        # Pixel data is unsigned char (8bit unsigned integer), and there are for (blue,green,red,alpha).
        # Unpack data from string into Python'y integers.
        b, g, r, a = struct.unpack_from("BBBB", img_data, offset=offset)
        # Assign BGRA color as RGBA.
        canvas.point(x, y, color=(r, g, b, a))

with open("test.png", "wb") as f:
    f.write(canvas.dump())