gpt4 book ai didi

python - 使用 GDAL 在 Python 和 Matplotlib 中读取 TIFF

转载 作者:行者123 更新时间:2023-12-01 22:40:40 24 4
gpt4 key购买 nike

我正在尝试使用 Python 和 MatPlotLib 显示灰度 TIFF 文件,

到目前为止,我已经阅读了以下文件:

import scipy as N
import gdal
import sys
import matplotlib.pyplot as pyplot

try:
tif = gdal.Open('filename.tif')
tifArray = tif.ReadAsArray()
except:
print 'The file does not exist.'
sys.exit(0)

band1 = tif.GetRasterBand(1)
band2 = tif.GetRasterBand(2)
band3 = tif.GetRasterBand(3)

band1Array = band1.ReadAsArray()
band2Array = band2.ReadAsArray()
band3Array = band3.ReadAsArray()

但是我不知道我还能做什么...我很无能。有人能帮我吗?

最佳答案

将文件处理为 2 数组后,您可以使用 matplotlib 中的任何函数来绘制 2D 数组,例如cmap、imshow 等

这是带有 marbles example 的输出

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

img=mpimg.imread('MARBLES.TIF ')
imgplot = plt.imshow(img)

如果您仅查看图像的 band3,您会得到以下结果:

imgplot2 = plt.imshow(band3Array)
plt.show()

band3 of marbles

进一步研究 MPL 和 2D 数组函数中的图像查看...

关于python - 使用 GDAL 在 Python 和 Matplotlib 中读取 TIFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com