I recommend to use rasterio if you want to process in python.
I didn't test the following code with multiband raster, but I hope the code will work with your data.
This code stretch raster values 0 to 254 range, set nodata 255, dtype Byte
(uint8
), and save without compression.
import rasterio
import numpy as np
import numpy.ma as ma
with rasterio.open('data/R022_32TQQ_20200309_B02.tif') as src:
array = src.read()
profile = src.profile
# prepare output array
output_array = np.empty(array.shape, dtype=np.uint8)
for idx, band_array in enumerate(array):
masked_array = ma.masked_array(band_array,
mask=(band_array==profile['nodata']))
min_ = masked_array.min()
max_ = masked_array.max()
# stretch value in 0-254 range
output_array[idx] = ((band_array - min_) / (max_ - min_) * 254).round().astype(np.uint8)
# set 255 at nodata pixel
output_array[idx][masked_array.mask] = 255
# Update metadata
profile.pop('compress', None)
profile.update(nodata=255, dtype='uint8')
# Write as GTiff
with rasterio.open('tmp/out.tif', 'w', **profile) as dst:
dst.write(output_array)
-scale_N
are set manually in the dialog? Or are they the real max-min of the data? Do you have these 100 images as 100 layers in QGIS?