import gdal
from numba import jit
import numpy as np
#读取tif数据集
def readTif(fileName):
dataset = gdal.Open(fileName)
if dataset == None:
print(fileName+"文件无法打开")
return dataset
#保存tif文件函数
def writeTiff(im_data,im_geotrans,im_proj,path):
if 'int8' in im_data.dtype.name:
datatype = gdal.GDT_Byte
elif 'int16' in im_data.dtype.name:
datatype = gdal.GDT_UInt16
else:
datatype = gdal.GDT_Float32
if len(im_data.shape) == 3:
im_bands, im_height, im_width = im_data.shape
elif len(im_data.shape) == 2:
im_data = np.array([im_data])
im_bands, im_height, im_width = im_data.shape
print(im_bands,im_width,im_height)
print(type(im_bands),type(im_width),type(im_height))
#创建文件
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(path, int(im_width), int(im_height), int(im_bands), datatype)
if(dataset!= None):
dataset.SetGeoTransform(im_geotrans) #写入仿射变换参数
dataset.SetProjection(im_proj) #写入投影
for i in range(im_bands):
dataset.GetRasterBand(i+1).WriteArray(im_data[i])
del dataset
dataset = readTif(r"I:\gf\gs_GF2_PMS2_E97.0_N32.9_20190810_L1A0004172802.tif")
im_width = dataset.RasterXSize #栅格矩阵的列数
im_height = dataset.RasterYSize #栅格矩阵的行数
im_geotrans = dataset.GetGeoTransform()#获取仿射矩阵信息
im_proj = dataset.GetProjection()#获取投影信息
im_data = dataset.ReadAsArray(0,0,im_width,im_height)#获取数据
print(im_data.shape)
im_data_BGR = im_data[0:3,:,]
print(im_data_BGR.shape)
@jit
def to8bit(data):
max_value = 0
for i in range(data.shape[0]):
for j in range(data.shape[1]):
for k in range(data.shape[2]):
if(data[i][j][k]>max_value):
max_value = data[i][j][k]
data_8bit = np.zeros(data.shape,np.uint8)
for i in range(data.shape[0]):
for j in range(data.shape[1]):
for k in range(data.shape[2]):
data_8bit[i][j][k] = int(data[i][j][k]*255/max_value)
return data_8bit
data_8bit = to8bit(im_data_BGR)
writeTiff(data_8bit,im_geotrans,im_proj,r"I:\gf2\8bitData\yushu_8bit.tif")