我正在尝试在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
Run Code Online (Sandbox Code Playgroud)
============================更新===================== ====================做了一些发现......我研究了使用统计规范化从一种数字格式转换为另一种数字格式的方法.我处理输入数据并使用以下算法将其转换为uint8:
std = elev_data.std() #standard dev …
Run Code Online (Sandbox Code Playgroud)Run Code Online (Sandbox Code Playgroud) 我正在使用python GDAL将栅格数据写入.tif文件.这是代码:
import numpy, sys
from osgeo import gdal, utils
from osgeo.gdalconst import *
# register all of the GDAL drivers
gdal.AllRegister()
# open the image
inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a1.tif",GDT_UInt16)
if inDs is None:
print "couldn't open input dataset"
sys.exit(1)
else:
print "opening was successful!"
cols = inDs.RasterXSize
rows = inDs.RasterYSize
bands = inDs.RasterCount
driver = inDs.GetDriver()
driver.Create("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif",cols,rows,3,GDT_UInt16)
outDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif")
if outDs is None:
print "failure to create new file"
sys.exit(1) …Run Code Online (Sandbox Code Playgroud)