Getting the color of a pixel

Last edited on

Requirements:

try:
    import __builtin__
except:
    import builtins as __builtin__
import os
import subprocess

def main():
    rgb = pixel_rgb("temp.png", 12, 12)
    test.compare(0, r)
    test.compare(0x65, g)
    test.compare(0x85, b)

def pixel_rgb(image_file_name, x, y):
    out = os_capture([
            "java",
            "-cp", squishinfo.testCase + "/..",
            "DumpPixels",
            image_file_name,
            "%s" % x,
            "%s" % y
        ]).strip()
    if out.find("Exception") != -1:
        test.log("Exception occurred: %s" % out, out)
        return None
    hex = out.split(" ")[2]
    p = __builtin__.int(hex, 16)
    r = (p & 0xff0000) >> 16
    g = (p & 0x00ff00) >> 8
    b = p & 0x0000ff
    return (r, g, b)

def os_capture(args, cwd=None):
    if cwd is None:
        cwd = os.getcwd()
    stdout = subprocess.Popen(
        args=args,
        cwd=cwd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT).communicate()[0]

    return stdout
test.py