OpenCV: Split target image into its red, green and blue channels

The code speaks for itself:

import sys
import os
import numpy as np
import cv2

def split_into_rgb_channels(image):
  '''Split the target image into its red, green and blue channels.
  image - a numpy array of shape (rows, columns, 3).
  output - three numpy arrays of shape (rows, columns) and dtype same as
           image, containing the corresponding channels. 
  red = image[:,:,2]
  green = image[:,:,1]
  blue = image[:,:,0]
  return red, green, blue

def main():
  ''' This function searches for a folder images/knowpapa subfolder, and splits
   all images found in that folder into its equivalent rgb channel. 
   It saves each image appending the terms 'red', 'green' and
  'blue' to the orginal filename.
  imagesdir = os.path.abspath(os.path.join(os.curdir, 'images', 'knowpapa'))
  print "Searching for images in {} Directory)".format(imagesdir)
  exts = ['.bmp', '.pbm', '.pgm', '.ppm', '.sr', '.ras', '.jpeg', '.jpg', 
    '.jpe', '.jp2', '.tiff', '.tif', '.png']
  for dirname, dirnames, filenames in os.walk(imagesfolder0):
    for filename in filenames:
      name, ext = os.path.splitext(filename)
      img = cv2.imread(os.path.join(dirname, filename))
      red, green, blue = split_into_rgb_channels(img)
      print img
      for values, color, channel in zip((red, green, blue), 
            ('red', 'green', 'blue'), (2,1,0)):
          img = np.zeros((values.shape[0], values.shape[1], 3), 
              dtype = values.dtype) 
          img[:,:,channel] = values
          print "Saving Image: {}.".format(name+color+ext)
          cv2.imwrite(os.path.join(dirname, name+color+ext), img)

if __name__ == "__main__":

Sample Output:
RGB split with OpenCV demo

Image courtesy: blaackhawk

By Bhaskar

My name is Bhaskar. I am a CTO and a startup techno guy with 10+ years of experience startups.
Full-time coding in Python, React, Java. Part-time coding in C++.
Interested in Music, Travelling

What excites me: anything that has the potential to disrupt the status quo.

Looking for technical support on a startup idea ?
write at : bhaskar {-at-}