Image subtraction using opencv and python
Question: I'm want to subtract one image from other.

This is what I have done so far.

    
    
    import cv2
    import numpy as np
    
    img1 = cv2.imread('template.jpg',0)
    img2 = cv2.imread('shot_one.jpg',0)
    
    img3 = img1-img2
    
    cv2.imshow('result',img3)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

I'm getting error saying:

    
    
    Traceback (most recent call last):
      File "E:/Python Programme/ATSS/subtra.py", line 7, in <module>
        img3 = img1-img2
    TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
    

I have followed [this
question](http://stackoverflow.com/questions/21425992/how-to-subtract-two-
images-using-python-opencv2-to-get-the-foreground-object). Any suggestions?


Answer: The error line `unsupported operand type(s) for -: 'NoneType' and 'int'`
clearly states that, One of your image path is invalid, Opencv doesn't throws
error, when the path is invalid, instead it returns a `None` Object.

    
    
    import cv2
    import numpy as np
    
    img1 = cv2.imread('template.jpg',0)
    img2 = cv2.imread('shot_one.jpg',0)
    
    if img1 and img2:
        img3 = img1-img2
    
        cv2.imshow('result',img3)
        cv2.waitKey()
        cv2.destroyAllWindows()
    else:
        print "Sorry the images weren't loaded properly."
    

