title: Keeping the dimensionality of image data stack in rgb2gray transformation
body: <p>I have 4D RGB image_data[image, height, width, channel], in my case the dimensions are (x, 32, 32, 3) and I would like to convert these images to grayscale so that I have still 4D so that my dimensions are (x, 32, 32, 1).</p>
  
  <p>I found a very simple rgb2gray transformation:</p>
  
  <pre><code>def rgb2gray(rgb):
      return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
  </code></pre>
  
  <p>The problem with this one is that it returns me (x, 32, 32), so I lose one dimension.</p>
  
  <p>Now the solution in for loops that I could think of is following and it works:</p>
  
  <pre><code>def rgb2gray(images):
      gray_images = []
      for image in images:
          gray_image = []
          for size in image:
              gray_size = []
              for channels in size:
                  channel = [np.dot(channels, [0.299, 0.587, 0.114])]
                  gray_size.append(channel)
              gray_image.append(gray_size)
          gray_images.append(gray_image)
      return np.array(gray_images)
  </code></pre>
  
  <p>Now, I wonder if there is some more magical way to achieve the same result and if there exists a wizard who can show it. Thank you.</p>