title: Convert a singleband Tif image to a pseudocolor singleband image using Python
body: <p>I have read through a lot of posts concerning this topic but I just could not find a solution for my problem. I have a raster image where I extracted the array with the values of the one band and normalized it to meet the range (0,255). However, when I use the PIL package it is not possible to convert the greyscale image to an image with the 'RGB' or 'P' mode where I can apply a color palette. Here is my python code:</p>
  <pre><code>from PIL import Image
  import numpy as np
  import rasterio
  
  file_path = &quot;./dem.tif&quot;
  with rasterio.open(file_path) as src:
      img = src.read(1) # read the one band
      img_cleaned = np.where(img&lt;1e-20,img*0,img) # pixels where no value is available were given a very small value which I remove here
      img_normalized = (img_cleaned - np.min(img_cleaned)) / (np.max(img_cleaned) - np.min(img_cleaned)) * 255.0 # normalize
      img_grey = Image.fromarray(img_normalized)
      img_color = img_grey.convert(&quot;P&quot;) # it is the same with img_color = img_grey.convert(&quot;RGB&quot;)
      
      img_color.show()
  
  </code></pre>
  <p>This is the histogram of the values of the array which proves that the values are in the right range:
  <a href="https://i.stack.imgur.com/419yy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/419yy.png" alt="Histogram of values of array" /></a></p>
  <p>And here is the image how it looks like even though I converted it:</p>
  <p><a href="https://i.stack.imgur.com/Bt2kf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bt2kf.png" alt="enter image description here" /></a></p>