我已经阅读了很多关于这个主题的帖子,但我就是找不到解决问题的办法。我有一个光栅图像,其中我提取了一个波段的值数组,并将其规格化以满足范围(0255)。但是,当我使用pil软件包时,无法将灰度图像转换为具有“rgb”或“p”模式的图像,在该模式下我可以应用调色板。以下是我的python代码:
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()
这是数组值的柱状图,证明值在正确的范围内:
下面是我转换后的图像:
暂无答案!
目前还没有任何答案,快来回答吧!