title: ValueError: x must consist of vectors of length 3 but has shape (480, 640, 4)
body: <p>I have this code which reduces a color to a minimal amount of colors to help identify what it is; it was working earlier, but now it says</p>
  
  <pre><code>ValueError: x must consist of vectors of length 3 but has shape (480, 640, 4)
  </code></pre>
  
  <p>Like I said it was working earlier, but then I tried adding 'white' because white turns to yellow and now it doesn't work at all</p>
  
  <pre><code>import numpy as np
  
  from matplotlib import colors
  from scipy.spatial import cKDTree as KDTree
  from scipy.misc import face
  from PIL import Image
  
  REDUCED_COLOR_SPACE = True
  
  # borrow a list of named colors from matplotlib
  if REDUCED_COLOR_SPACE:
      use_colors = {k: colors.cnames[k] for k in ['red', 'green', 'blue', 'black', 'yellow', 'purple']}
  else:
      use_colors = colors.cnames
  
  # translate hexstring to RGB tuple
  named_colors = {k: tuple(map(int, (v[1:3], v[3:5], v[5:7]), 3*(16,)))
                  for k, v in use_colors.items()}
  ncol = len(named_colors)
  
  
  if REDUCED_COLOR_SPACE:
      ncol -= 1
      no_match = named_colors.pop('purple')
  else:
      no_match = named_colors['purple']
  
  # make an array containing the RGB values 
  color_tuples = list(named_colors.values())
  color_tuples.append(no_match)
  color_tuples = np.array(color_tuples)
  
  color_names = list(named_colors)
  color_names.append('no match')
  
  # get example picture
  img = Image.open('apple.png')
  
  # build tree
  tree = KDTree(color_tuples[:-1])
  # tolerance for color match `inf` means use best match no matter how
  # bad it may be
  tolerance = np.inf
  # find closest color in tree for each pixel in picture
  dist, idx = tree.query(img, distance_upper_bound=tolerance)
  # count and reattach names
  counts = dict(zip(color_names, np.bincount(idx.ravel(), None, ncol+1)))
  
  print(counts)
  
  import pylab
  
  pylab.imshow(img)
  pylab.savefig('apple.png')
  pylab.clf()
  pylab.imshow(color_tuples[idx])
  pylab.savefig('minimal.png' if REDUCED_COLOR_SPACE else 'reduced.png')
  </code></pre>
  
  <p>It did work before:</p>
  
  <p><a href="https://i.stack.imgur.com/PvNgF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PvNgF.png" alt="Before"></a></p>
  
  <p>And it did work after:</p>
  
  <p><a href="https://i.stack.imgur.com/jM2LK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jM2LK.png" alt="After"></a></p>
  
  <p><strong>The problem is:</strong><br>
  When I run it, I get this error </p>
  
  <blockquote>
    <p>'line 45, in  dist, idx = tree.query(img, distance_upper_bound=tolerance)<br>
    File "ckdtree.pyx", line 754, in scipy.spatial.ckdtree.cKDTree.query<br>
    ValueError: x must consist of vectors of length 3 but has shape (480, 640, 4)' </p>
  </blockquote>
  
  <p>I expect it to take a picture I choose and change it like in the example I posted above.</p>
  
  <p>How can I realize this?</p>