使用`gdal`将大型彩色图像保存为`GTiff` [英] Saving a large color image as `GTiff` with `gdal`
问题描述
我正在尝试保存大小(15000,80000,3)
的大图片。这个数组是一个numpy数组,我初始化为 im_final = np.zeros((15000,80000,,3))
。要进行保存,我使用 gdal
,如下所示:
I'm trying to save a large image of size (15000, 80000, 3)
. This array is a numpy array that I initialized as im_final = np.zeros((15000,80000,,3))
. To do the saving, I use gdal
like so:
dst_ds = gdal.GetDriverByName('GTiff').Create('val.tif', 80000, 15000, 3, gdal.GDT_Byte)
dst_ds.GetRasterBand(1).WriteArray(im_final[:,:,0]) # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(im_final[:,:,1]) # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(im_final[:,:,2]) # write b-band to the raster
dst_ds.FlushCache() # write to disk
dst_ds = None
保存时,生成的图像为黑白色。但是,我需要图像为RGB,有谁知道问题是什么?此外, im_final
中的值为 uint16
。
When I save it, the resulting image is black and white. However, I need the image to be RGB, does anyone know what the issue is? Furthermore, the values in im_final
are uint16
.
推荐答案
问题是你试图将 uint16
写入 uint8
( gdal.GDT_Byte
)图片。如果您确实需要一个8位图像(例如,如果您想在非GIS程序中查看此图像),最佳做法是将 im_final
缩放到0- 255。这可以是从0-65535到0-255或每个波段的最小/最大值到0-255或任何其他方式的映射。
The problem is you are trying to write uint16
into a uint8
(gdal.GDT_Byte
) image. If you really need a 8-bit image (such as if you want to view this image in non-GIS programs), the best practice is to scale im_final
to between 0-255. This could be a mapping from 0-65535 to 0-255 or min/max of each band to 0-255 or any other number of ways.
如果值在在 driver.Create()$ $ c>。
这篇关于使用`gdal`将大型彩色图像保存为`GTiff`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!