Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sameAs() for images #48

Closed
nickwender opened this issue Sep 13, 2013 · 1 comment
Closed

sameAs() for images #48

nickwender opened this issue Sep 13, 2013 · 1 comment

Comments

@nickwender
Copy link

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
Copy link
Owner

3080f5f includes a slightly modified version of these methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants