Reputation: 19
My code is:
import cv2
from PIL import Image
import numpy as np
img=cv2.imread("IMG040.jpg")
img2=cv2.imread("IMG040.jpg")
p1 = np.array(img)
p2 = np.array(img2)
img3=img-img2
p3 = np.array(img3)
if p3==0 :
print "the same"
else:
print"not the same"
but I have this problem
File "part2.py", line 10, in <module>
if p3==0 :
Error message:
**ValueError:** The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
Upvotes: 0
Views: 313
Reputation: 32511
The expression
p3==0
Creates a boolean numpy array. The Python if
statement does not know how to interpret this whole array as being true or false. That's what the error message means. You presumably want to know if all of the elements are zero and that is why the error message suggests you should use all()
.
To do this, you would end up changing the line to
if (p3==0).all():
However it is better to compare numpy arrays with the allclose
method, which can account for numerical errors. So try replacing this
img3=img-img2
p3 = np.array(img3)
if p3==0 :
print "the same"
else:
print"not the same"
with
if np.allclose(img, img2):
print "the same"
else:
print "not the same"
Upvotes: 1
Reputation: 86
You need to do the if statement like this:
if np.all(p3==0):
print 'The same'
else:
print 'Not the same'
Upvotes: 0