我正在尝试使用Python中的GDAL创建一个.tif文件。它创建了一个文件,但是每当我浏览它时,都会显示“没有可用的预览”。目前,我只是想让它复制输入文件。以下是我的代码:
gdal.AllRegister()
inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a2.tif")
if inDs is None:
print 'Could not open image file'
sys.exit(1)
else:
print "successfully opened input file"
rows = inDs.RasterYSize
cols = inDs.RasterXSize
myband = inDs.GetRasterBand(1)
elev_data = myband.ReadAsArray(0,0,cols,rows)
driver = inDs.GetDriver()
outDs = driver.Create('C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\new.tif', cols, rows, 1, GDT_Int32)
if outDs is None:
print "couldn't open output file"
sys.exit(1)
outBand = outDs.GetRasterBand(1)
outData = numpy.zeros((rows,cols),numpy.int16)
outBand.WriteArray(elev_data)
outBand.FlushCache()
outBand.SetNoDataValue(-99)
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())
del outData
我做了一些发现...... 我研究了使用统计规范化将数字格式从一个转换为另一个的方法。我使用以下算法处理输入数据并将其转换为uint8:
std = elev_data.std() #standard dev
avg = elev_data.mean()
arr = numpy.zeros((rows,cols),numpy.uint8)
for _i_ in _range_(_rows_):
for _j_ in _range_(_cols_):
arr[i,j] = (((out_elev[i,j]-avg)/std)*127)+128 #normalization formula
#this puts all vals in range 1 to 255 (uint8)
dr = gdal.GetDriverByName("GTiff")
outDs = dr.Create("name",cols,rows,3,GDT_Byte)
#creates and RGB file, accepts uint8 for input
outDs.GetRasterBand(1).WriteArray(arr) #write the output as shades of red
#this writes out a format viewable by microsoft products
我想复制的主要原因是为了证明我能够读取,然后根据计算写出更新的数据。
有什么方法可以使用颜色渐变来输出数据,而不是只用一种颜色的阴影?