Skip to content

Commit

Permalink
Upadated thumbnail code for interface;
Browse files Browse the repository at this point in the history
  • Loading branch information
adityasevak123ga committed Nov 30, 2023
1 parent 2e7f9df commit 2ac248e
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions uncoverml/interface_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,28 @@ def calc_std(config):
dst.write(std_data, 1)


def normalize(arr):
'''
Linear normalization
http://en.wikipedia.org/wiki/Normalization_%28image_processing%29
'''
arr = arr.astype('float')
# Do not touch the alpha channel
for i in range(3):
min_val = arr[..., i].min()
max_val = arr[..., i].max()
if min_val != max_val:
arr[..., i] -= min_val
arr[..., i] *= (255.0/(max_val-min_val))
return arr
def stretch_raster(data, pct_lims=[5, 95]):
percentiles = np.nanpercentile(np.ravel(data), pct_lims)
percentiles = percentiles.tolist()
lower_pct = percentiles[0]
upper_pct = percentiles[1]

data = data.astype(float)
data = (data - lower_pct) / (upper_pct - lower_pct)
data[np.isnan(data)] = 0
data = np.maximum(np.minimum(data * 255, 255), 0)
return data


def create_thumbnail(config, res_type):
res_dir = Path(config.output_dir)
in_file = res_dir / f'{res_type}.tif'
img = Image.open(str(in_file)).convert('RGBA')
with rasterio.open(str(in_file), 'r+') as src:
src.nodata = np.nan
data = src.read(1)

arr = np.array(img)
out_image = Image.fromarray(normalize(arr).astype('uint8'), 'RGBA')
stretched_data = stretch_raster(data)
out_image = Image.fromarray(stretched_data.astype('unit8'), 'L')
out_file = res_dir / f'{res_type}_thumbnail.png'
out_image.save(str(out_file))
print(str(out_file))
Expand Down

0 comments on commit 2ac248e

Please sign in to comment.