python - 使用 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()
这是图像,即使我转换了它:
解决方案
推荐阅读
- python - 可以将矩阵作为 Keras 嵌入层的输入吗?
- php - 在使用模态 PHP HTML 时似乎无法获得输入的值
- vue.js - 将道具/属性传递给自定义组件内的嵌套组件
- git - 将下载的 git 存储库与克隆的存储库合并
- java - 无法编译的源代码 - 表达式的非法开始
- shell - 用于提取正在运行的 sid 的 shell 脚本
- javascript - 如何在 JS 模块中定义错误类
- android - 长按像paytm和swiggy这样的Android应用程序图标后如何显示菜单选项?
- python - Keras 模型 evaluate() 和 predict_classes() 给出了相互矛盾的结果
- reactjs - 在 React 中清除 Apollo GraphQL 客户端中的特定查询缓存