Spaces:
Sleeping
Sleeping
from osgeo import gdal | |
import numpy as np | |
import os | |
import tempfile | |
from PIL import Image | |
def convert_gtiff_to_8bit(src): | |
dst = src | |
img = gdal.Open(src) | |
driver = gdal.GetDriverByName('GTiff') | |
output_image = driver.Create(dst, img.RasterXSize, img.RasterYSize, img.RasterCount, gdal.GDT_Byte, ['PHOTOMETRIC = RGB']) | |
output_image.SetGeoTransform(img.GetGeoTransform()) | |
output_image.SetProjection(img.GetProjection()) | |
max_bands = img.RasterCount | |
for i in range(max_bands): | |
i = i+1 | |
band = img.GetRasterBand(i) | |
band_array = band.ReadAsArray() | |
min, max = band.ComputeRasterMinMax(1) | |
band_array = np.interp(band_array, (min,max), (0,255)).astype(np.uint8) | |
out = output_image.GetRasterBand(i) | |
out.WriteArray(band_array) | |
out.FlushCache() | |
return output_image | |
del output_image | |