Reputation: 117
Here is my short code:
from cv2 import cv2
img = cv2.imread("1.png")
print("Высота:"+str(img.shape[0]))
print("Ширина:" + str(img.shape[1]))
print("Количество каналов:" + str(img.shape[2]))
if img[0, 0] == (255, 255, 255):
print("Yes")
But I have an error:
Traceback (most recent call last):
File "e:\Projects\Captcha Decrypt\main.py", line 9, in <module>
if img[0, 0] == [255, 255, 255]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Can you solve this problem?
Upvotes: 0
Views: 49
Reputation: 337
Just change your img[0][0]
in list(img[0][0])
:
from cv2 import cv2
img = cv2.imread("1.png")
print("Высота:"+str(img.shape[0]))
print("Ширина:" + str(img.shape[1]))
print("Количество каналов:" + str(img.shape[2]))
if list(img[0][0]) == [255, 255, 255]:
print("Yes")
else:
print("No")
Upvotes: 2
Reputation: 2449
Your object img
is a 3D array. This means that img[x,y] will be a vector, so if you try to compare it, the comparison will check the condition for each element in the vector.
As it is an array, the return will also be an array array([True, True])
if the condition matches.
The error message you got already gave you a valid solution that you can use: call .all() on the resulting vector, which will check whether the condition is true for all elements in the array.
The correct piece of code would be:
if (img[0, 0] == (255, 255, 255)).all():
print("Yes")
Another possible solution would be using numpy array equality (cv2
uses numpy for arrays so it works for you too):
import numpy as np
if np.logical_all(img[0, 0] == (255, 255, 255)):
print("Yes")
Upvotes: 1