dtmilano / AndroidViewClient

Android ViewServer and ADB client
Apache License 2.0
1.63k stars 347 forks source link

sameAs() for images #48

Closed nickwender closed 11 years ago

nickwender commented 11 years ago

I didn't want to do a pull request because I only have an informal piece of code.

Here is a method to compare images returned by adbclient.takeSnapshot(). It provides nearly the same functionality as MonkeyImage.sameAs(). Instead of calling image1.sameAs(image2, 0.95), you call sameAs(image1, image2, 0.95). This new sameAs() function could easily be made a method of AdbClient.


# Requires Image from PIL
# Which should have been imported by the call to takeSnapshot()

def percentSame(image1, image2):
    # If the images differ in size, return 0% same.
    size_x1, size_y1 = image1.size
    size_x2, size_y2 = image2.size
    if (size_x1 != size_x2 or
        size_y1 != size_y2):
        return 0

    # Images are the same size
    # Return the percent of pixels that are equal.
    numPixelsSame = 0
    numPixelsTotal = sizze_x1 * size_y1
    image1Pixels = image1.load()
    image2Pixels = image2.load()

    # Loop over all pixels, comparing pixel in image1 to image2
    for x in range(size_x1):
        for y in range(size_y1):
            if (image1Pixels[x, y] == image2Pixels[x, y]):
                numPixelsSame += 1

    return float(numPixelsSame) / float(numPixelsTotal)

def sameAs(image1, image2, percent=1.0):
    percentSame = percentSame(image1, image2)
    return (percentSame >= percent)
dtmilano commented 11 years ago

3080f5f70a8ab8f3592a7435b2c45a3eff90007a includes a slightly modified version of these methods.