Tried to split image and kept getting error message

i was trying to split an image into red channel,blue channel and green channel, using cv2.split() function. i keep getting an error message. that says Traceback (most recent call last):
File “C:\Users\david\AppData\Local\Programs\Python\Python36-32\p99.py”, line 5, in
(R,G,B) = cv2.split(q);
ValueError: not enough values to unpack (expected 3, got 0)

how do i eliminate this error message.
i tried reinstalling opencv. i tried reinstalling idle. i tried system restore.

your input (q) is invalid, try to find out why

>>> q = None
>>> cv2.split(q)
[]
>>> (r,g,b) = cv2.split(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 0)

I forgot to write my code. I was under a lot of stress. My code is

import cv2

q = cv2.imread(‘mm.PNG’);
w = cv2.split(q);

My code is,

import cv2

q = cv2.imread(‘mm.PNG’);
(R,G,B) = cv2.split(q);

I zoned out the first time and wrote the code wrong the first. I am sure it is right the second time.

Here is my code,

import cv2

q = cv2.imread(‘mm.PNG’);
(R,G,B) = cv2.split(q);

I am sure it is right this time.

this is the main failure. your image did not load.
‘mm.PNG’ is not, where your program starts
imread() returns None in that case.
you should add a check like :
if (np.shape(img)==()): #read error, you cant go on !
after each imread(), to be safe !

will return an empty [] then (again, see example above !),
and you cannot unpack that to a rgb tuple
(which would be bgr order in opencv anyway)

Thanks for the advice

in fact the result is None, so you can also check:

if img is None: # that's bad
    pass # do something about it

or just check and fail:

assert img is not None
1 Like