import os
from osgeo import gdal
import numpy as np
from PIL import Image
from spectral import *
def get_img(dataset_img):
width = dataset_img.RasterXSize
height = dataset_img.RasterYSize
bands = dataset_img.RasterCount
proj = dataset_img.GetProjection()
geotrans = dataset_img.GetGeoTransform()
img = dataset_img.ReadAsArray(0, 0, width, height)
new_img = np.transpose(img, (1, 2, 0))
return new_img
def tiftopng(input_folder,output_folder,band=[9,3,2]):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for file_name in os.listdir(input_folder):
if file_name.endswith('.tif'):
input_file = os.path.join(input_folder, file_name)
image = get_img(gdal.Open(input_file))
output_file = os.path.join(output_folder, file_name.replace('.tif', '.png'))
spectral.settings.WX_GL_DEPTH_SIZE = 16
save_rgb(output_file, image, bands=band, format='png')
input_folder = r'F:\datasets\ZY\ZY_S2_TEST\540\Z'
output_folder = r'F:\datasets\ZY\ZY_S2_TEST\540\Z_png'
tiftopng(output_folder,input_folder)
if __name__ == '__main__':
root = r'F:\datasets\ZY\ZY_S2_TEST\\540'
x_path = root + '\X'
y_path = root + '\Y'
z_path = root + '\Z'
out_root = 'F:\datasets\ZY\ZY_S2_TEST\\540'
out_x = out_root + '\X_png'
out_y = out_root + '\Y_png'
out_z = out_root + '\Z_png'
tiftopng(y_path, out_y,band=[9,3,2])
tiftopng(z_path, out_z,band=[29,3,2])
tiftopng(x_path, out_x,band=[9,3,2])