Beispiel #1
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 #2
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