'I m trying to convert the pixels which is painted black to another color but ı got an error l

import cv2
import numpy as np

image = cv2.imread('images1.png')

ss=[]

for x in range (0,260,1):

    for y in range(0,190,1):

        if image[y,x]==[0,0,0]:

            image[y,x]=[0,255,162]

cv2.imshow("image",image)

cv2.waitKey(0)

Error:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()



Solution 1:[1]

you're trying to compare an array with another when you call img[y,x]==[0,0,0] you got a vector of 3 logicals (supose [True,True,True] or any other variation). You have to compress this to an unique logical: you can use .all() to that (black means that the cannel red ==0 , the blue==0 and green==0, so you need that the 3 vals to be True to enter the if)

so the solution will be to replace

  if image[y,x]==[0,0,0]:

with

  if (image[y,x]==[0,0,0]).all():

Solution 2:[2]

If you want to do this fast, you don't want to do this with a loop – instead, use cv2.inRange to get a mask "image" of pixels matching your color, then paint them all black with a Numpy broadcast assignment:

import cv2

image = cv2.imread('image.png')
src_color = (0, 0, 0)

# Compute matching pixels
mask = cv2.inRange(image, src_color, src_color)

# Replace pixels where the mask is nonzero
image[mask > 0] = [0, 255, 162]

cv2.imshow("image", image)

cv2.waitKey(0)

This turns this:
enter image description here
into this:
enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ulises Bussi
Solution 2