使用python将单波段tif图像转换为伪彩色单波段图像

kzipqqlq  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(319)

我已经阅读了很多关于这个主题的帖子,但我就是找不到解决问题的办法。我有一个光栅图像,其中我提取了一个波段的值数组,并将其规格化以满足范围(0255)。但是,当我使用pil软件包时,无法将灰度图像转换为具有“rgb”或“p”模式的图像,在该模式下我可以应用调色板。以下是我的python代码:

  1. from PIL import Image
  2. import numpy as np
  3. import rasterio
  4. file_path = "./dem.tif"
  5. with rasterio.open(file_path) as src:
  6. img = src.read(1) # read the one band
  7. 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
  8. img_normalized = (img_cleaned - np.min(img_cleaned)) / (np.max(img_cleaned) - np.min(img_cleaned)) * 255.0 # normalize
  9. img_grey = Image.fromarray(img_normalized)
  10. img_color = img_grey.convert("P") # it is the same with img_color = img_grey.convert("RGB")
  11. img_color.show()

这是数组值的柱状图,证明值在正确的范围内:

下面是我转换后的图像:

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题