0

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?

2 Answers 2

2

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.