The arcgis loading image shows that the pixel value is a floating point type, which makes it impossible to convert the image digits (16bit to 8bit)

The situation is as follows:
Insert picture description here
the pixel values ​​of this 16-bit image are all floating-point types. No matter if it is converted to 8bit by arcgis or the code, the result will be blank or completely black. It needs to be converted to unsigned 16bit and then converted to 8bit to display normally.

# -*- coding: utf-8 -*-
from osgeo import ogr
from osgeo import gdal
from gdalconst import *
import os, sys, time
import numpy as np
np.seterr(divide='ignore',invalid='ignore')

def read_img(filename):
    dataset=gdal.Open(filename)

    im_width = dataset.RasterXSize
    im_height = dataset.RasterYSize

    im_geotrans = dataset.GetGeoTransform()
    im_proj = dataset.GetProjection()

    # band1 = dataset.GetRasterBand(1)
    # band2 = dataset.GetRasterBand(2)
    # band3 = dataset.GetRasterBand(3)
    # band4 = dataset.GetRasterBand(4)
    # band1.SetNoDataValue(0)
    # band2.SetNoDataValue(0)
    # band3.SetNoDataValue(0)
    # band4.SetNoDataValue(0)

    im_data = dataset.ReadAsArray(0,0,im_width,im_height)

    del dataset 
    return im_proj,im_geotrans,im_width, im_height,im_data


def write_img(filename, im_proj, im_geotrans, im_data):
    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
    else:
        im_bands, (im_height, im_width) = 1,im_data.shape 

    driver = gdal.GetDriverByName("GTiff")
    dataset = driver.Create(filename, im_width, im_height, im_bands, gdal.GDT_UInt16)

    dataset.SetGeoTransform(im_geotrans)
    dataset.SetProjection(im_proj)

    if im_bands == 1:
        dataset.GetRasterBand(1).WriteArray(im_data)
    else:
        for i in range(im_bands):
            dataset.GetRasterBand(i+1).WriteArray(im_data[i])

    del dataset

#拉伸
def stretch_n(bands, img_min, img_max, lower_percent=2, higher_percent=98):
    out = np.zeros_like(bands).astype(np.float32)
    a = img_min
    b = img_max
    c = np.percentile(bands[:, :], lower_percent)
    d = np.percentile(bands[:, :], higher_percent)
    t = a + (bands[:, :] - c) * (b - a) / (d - c)
    t[t < a] = a
    t[t > b] = b
    out[:, :] = t
    return out


if __name__ == "__main__":
    in_path = "E:/hz/hz_autumn.tif"
    out_path = "E:/hz/hz_autumn_int.tif"

    im_proj,im_geotrans,im_width, im_height,im_data = read_img(in_path)
    # temp = im_data[0] + im_data[1] + im_data[2] + im_data[3]
    #temp = im_data[0]
    # temp[temp>0] = 1
    # im = im_data[0]
    # temp = stretch_n(temp, 0, 255)
    # im = np.uint8(im)
    write_img(out_path, im_proj, im_geotrans, temp)

Note here dataset = driver.Create(filename, im_width, im_height, im_bands, gdal.GDT_UInt16)

Guess you like

Origin blog.csdn.net/qq_20373723/article/details/107354494