title: Images are changed to different colors (with pillow), how to get it back to the original colors?
body: <p>I am trying to find the dominant color in a frame in a video. This works well, however, my frames are somehow converted into different colors. Yellow/pink becomes blue/purple-ish, but black and white stay the same (thus it is not the inverted colors).</p>
  <p>Does anyone know where it comes from and how I can change it so that the original colors are kept? This is my code:</p>
  <pre><code>import cv2
  from sklearn.cluster import KMeans
  from collections import Counter
  from PIL import Image
  import matplotlib.pyplot as plt
  import matplotlib.patches as patches
  
  video = cv2.VideoCapture('video.mp4')
  
  def show_blurred_image(image, dominant_color):
      frame_to_blur = Image.fromarray(image)
      
      blurred_frame = cv2.blur(image, (200,200))
      blurred_frame = Image.fromarray(blurred_frame)
      
      plt.subplot(121),plt.imshow(frame_to_blur),plt.title('Original')
      plt.xticks([]), plt.yticks([])
      plt.subplot(122),plt.imshow(blurred_frame),plt.title('Blurred')
      plt.xticks([]), plt.yticks([])
  
      
      R = round(dominant_color[0])
      G = round(dominant_color[1])
      B = round(dominant_color[2])
  
      custom_color = '#%02x%02x%02x' % (R, G, B)
      print(custom_color)
      rect = patches.Rectangle((1620,0),300,1080,linewidth=1,
                               fill = True,
                               edgecolor=custom_color,
                               facecolor=custom_color)
      ax = plt.gca()
      ax.add_patch(rect)
      
      plt.show()
      
  
  def get_dominant_color(image, k=4, image_processing_size = None):
      &quot;&quot;&quot;
      takes an image as input
      returns the dominant color of the image as a list
      
      dominant color is found by running k means on the 
      pixels &amp; returning the centroid of the largest cluster
  
      processing time is sped up by working with a smaller image; 
      this resizing can be done with the image_processing_size param 
      which takes a tuple of image dims as input
  
      &gt;&gt;&gt; get_dominant_color(my_image, k=4, image_processing_size = (25, 25))
      [56.2423442, 34.0834233, 70.1234123]
      &quot;&quot;&quot;
      #resize image if new dims provided
      if image_processing_size is not None:
          image = cv2.resize(image, image_processing_size, 
                              interpolation = cv2.INTER_AREA)
      
      #reshape the image to be a list of pixels
      image = image.reshape((image.shape[0] * image.shape[1], 3))
  
      #cluster and assign labels to the pixels 
      clt = KMeans(n_clusters = k)
      labels = clt.fit_predict(image)
  
      #count labels to find most popular
      label_counts = Counter(labels)
  
      #subset out most popular centroid
      dominant_color = clt.cluster_centers_[label_counts.most_common(1)[0][0]]
  
      return list(dominant_color)
  
  dominant_colors = []
  show_frame = 10
  frame_nb = 0
  
  while(video.isOpened()):
      ret, frame = video.read()
      
      if ret == True: 
          if (frame_nb == show_frame):
              dominant_color = get_dominant_color(frame)
              show_blurred_image(frame, dominant_color)
      
          frame_nb += 1
      else:
          break
      
          
  video.release()
  cv2.destroyAllWindows()
  </code></pre>