python script that captures an image and compares it to another image
Question: Basically, I want to automate something. I would capture a 100x50 picture of a
certain button, and have a script that takes a picture of the same area with
the same size, and then compares the two pictures to see if they are
different. If they are different, it would do nothing, if they are similar +/-
a certain amount, it would then click it.

I have the screen capture kind of worked out, I just don't know how to compare
them with PIL. I have done a lot of googling without success.


Answer: You can use `pylab.imread` to easily read an image into a NumPy array:

    
    
    In [3]: import scipy, pylab
    
    In [4]: x = pylab.imread('example.jpg')
    
    In [5]: x.shape
    Out[5]: (480, 640, 3)
    

You can view the image, or any subset thereof:

    
    
    In [6]: pylab.imshow(x)
    Out[6]: <matplotlib.image.AxesImage at 0x98e564c>
    
    In [7]: pylab.show()
    

If you wanted to compare two 8-by-8 blocks (say, the top left block of the red
and blue layers), you could compute the mean squared error:

    
    
    In [8]: x[:8,:8,0]
    Out[8]: 
    array([[147, 143, 146, 144, 146, 148, 146, 149],
           [145, 142, 146, 145, 147, 149, 148, 151],
           [143, 141, 146, 145, 147, 147, 148, 150],
           [143, 143, 146, 146, 146, 145, 147, 148],
           [147, 147, 147, 148, 147, 145, 146, 146],
           [146, 147, 145, 147, 148, 145, 147, 146],
           [146, 147, 144, 147, 147, 144, 146, 144],
           [147, 148, 144, 147, 147, 144, 146, 144]], dtype=uint8)
    
    In [9]: x[:8,:8,1]
    Out[9]: 
    array([[125, 121, 122, 120, 118, 120, 116, 120],
           [123, 120, 122, 122, 119, 121, 118, 122],
           [122, 120, 123, 122, 120, 120, 118, 121],
           [122, 122, 123, 122, 120, 118, 117, 119],
           [124, 123, 123, 124, 121, 119, 119, 119],
           [122, 123, 120, 122, 121, 119, 119, 119],
           [121, 122, 116, 119, 119, 117, 119, 117],
           [122, 122, 115, 118, 119, 116, 119, 117]], dtype=uint8)
    
    In [10]: def mse(x, y):
       ....:     return scipy.mean((x.astype(float)-y)**2)
    
    In [11]: mse(x[:8,:8,0], x[:8,:8,1])
    Out[11]: 676.0625
    

