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:
from PIL import Image
import numpy as np
import rasterio
file_path = "./dem.tif"
with rasterio.open(file_path) as src:
img = src.read(1) # read the one band
img_cleaned = np.where(img<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("P") # it is the same with img_color = img_grey.convert("RGB")
img_color.show()
This is the histogram of the values of the array which proves that the values are in the right range:
And here is the image how it looks like even though I converted it: