如何解决使用 Python 将单波段 Tif 图像转换为伪彩色单波段图像
我已经阅读了很多关于这个主题的帖子,但我就是找不到解决我的问题的方法。我有一个光栅图像,我用一个波段的值提取了数组并将其归一化以满足范围 (0,255)。但是,当我使用 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()
这是我转换后的图像:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。