python - 如何在 python 中编写/创建 GeoTIFF RGB 图像文件?

标签 python numpy rgb gdal geotiff

我有 5 个形状为 nx, ny 的 numpy 数组

lons.shape = (nx,ny)
lats.shape = (nx,ny)
reds.shape = (nx,ny)
greens.shape = (nx,ny)
blues.shape = (nx,ny)

红色、绿色和蓝色数组包含 0–255 范围内的值,纬度/经度数组是纬度/经度像素坐标。

我的问题是如何将这些数据写入 geotiff?

我最终想使用 basemap 绘制图像。

这是我到目前为止的代码,但是我得到了一个巨大的 GeoTIFF 文件(~500MB)并且它是空白的(只是一个黑色图像)。还要注意 nx, ny = 8120, 5416。

from osgeo import gdal
from osgeo import osr
import numpy as np
import h5py
import os

os.environ['GDAL_DATA'] = "/Users/andyprata/Library/Enthought/Canopy_64bit/User/share/gdal"

# read in data
input_path = '/Users/andyprata/Desktop/modisRGB/'
with h5py.File(input_path+'red.h5', "r") as f:
    red = f['red'].value
    lon = f['lons'].value
    lat = f['lats'].value

with h5py.File(input_path+'green.h5', "r") as f:
    green = f['green'].value

with h5py.File(input_path+'blue.h5', "r") as f:
    blue = f['blue'].value

# convert rgbs to uint8
r = red.astype('uint8')
g = green.astype('uint8')
b = blue.astype('uint8')

# set geotransform
nx = red.shape[0]
ny = red.shape[1]
xmin, ymin, xmax, ymax = [lon.min(), lat.min(), lon.max(), lat.max()]
xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)

# create the 3-band raster file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 3, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geotransform)    # specify coords
srs = osr.SpatialReference()            # establish encoding
srs.ImportFromEPSG(3857)                # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(r)   # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(g)   # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(b)   # write b-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None                           # save, close

最佳答案

我认为问题是当您创建数据集时,您将 GDT_Float32 传递给它。对于像素范围为 0-255 的标准图像,您需要 GDT_Byte。 Float 通常要求值介于 0-1 之间。

我获取了您的代码并随机生成了一些数据,以便我可以测试您 API 的其余部分。然后我在太浩湖周围创建了一些虚拟坐标。

这是代码。

#!/usr/bin/env python
from osgeo import gdal
from osgeo import osr
import numpy as np
import os, sys

#  Initialize the Image Size
image_size = (400,400)

#  Choose some Geographic Transform (Around Lake Tahoe)
lat = [39,38.5]
lon = [-120,-119.5]

#  Create Each Channel
r_pixels = np.zeros((image_size), dtype=np.uint8)
g_pixels = np.zeros((image_size), dtype=np.uint8)
b_pixels = np.zeros((image_size), dtype=np.uint8)

#  Set the Pixel Data (Create some boxes)
for x in range(0,image_size[0]):
    for y in range(0,image_size[1]):
        if x < image_size[0]/2 and y < image_size[1]/2:
            r_pixels[y,x] = 255
        elif x >= image_size[0]/2 and y < image_size[1]/2:
            g_pixels[y,x] = 255
        elif x < image_size[0]/2 and y >= image_size[1]/2:
            b_pixels[y,x] = 255
        else:
            r_pixels[y,x] = 255
            g_pixels[y,x] = 255
            b_pixels[y,x] = 255

# set geotransform
nx = image_size[0]
ny = image_size[1]
xmin, ymin, xmax, ymax = [min(lon), min(lat), max(lon), max(lat)]
xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)

# create the 3-band raster file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 3, gdal.GDT_Byte)

dst_ds.SetGeoTransform(geotransform)    # specify coords
srs = osr.SpatialReference()            # establish encoding
srs.ImportFromEPSG(3857)                # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(r_pixels)   # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(g_pixels)   # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(b_pixels)   # write b-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None

这是输出。 (注意:目标是产生颜色,而不是地形!)

enter image description here

这是 QGIS 中的图像,验证了投影。

enter image description here

关于python - 如何在 python 中编写/创建 GeoTIFF RGB 图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537599/

相关文章:

python - 从数组中选择元素以最小化内存使用

python - 使用Power方法从Python中的3x3矩阵获取特征值

c++ - 在openCV中访问特定图像中所有像素的RGB值

javascript - 从ajax驱动的网站检索渲染的html

python - 正则表达式 : matching integers inside of brackets

python - 从 AWS Lambda/tmp 目录导入 python 包

css - 将十六进制反转为 rgba() 以获得叠加颜色所需的不透明度

python - 如何使用填充沿新维度重复 numpy 数组?

python - 如何为每个term保留k个最相似的term记录,并将不太相似的用0替换

ios - 如何选择十六进制形式或 RGB 形式的颜色,而不是使用 Swift 中给我的颜色