As explained on OpenCV's website:
The shape of an image is accessed by img.shape. It returns a tuple of the number of rows, columns, and channels (if the image is color)
If an image is grayscale, the tuple returned contains only the number of rows and columns
Your code unpacks 2 values from the result of img.shape, so it works only for grayscale images.
If your image is in color, you should also unpack the third value. Otherwise you get the error that you ran into because you're trying to unpack 2 values when img.shape gives you 3 values.
The following will work for images with color:
rows, cols, channels = img.shape
However, as advised on OpenCV's website:
it is a good method to check whether the loaded image is grayscale or color.
That way, you'll know how many values to unpack and your program will work for both grayscale and color images.