Skip to content

Commit b8121aa

Browse files
committed
Quickfix: thumbnail maker's arr dims
1 parent b22ab7b commit b8121aa

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

notebooks/omega_cube_viz.py

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import marimo
22

3-
__generated_with = "0.15.5"
3+
__generated_with = "0.19.8"
44
app = marimo.App(width="medium")
55

66

@@ -153,7 +153,9 @@ def select_for_data_cubes(
153153

154154
@app.cell
155155
def _(mo):
156-
mo.md(r"""# OMEGA C Channel Data""")
156+
mo.md(r"""
157+
# OMEGA C Channel Data
158+
""")
157159
return
158160

159161

@@ -242,7 +244,9 @@ def _(omega_c_chan_ds, plt, select_rgb_img):
242244

243245
@app.cell
244246
def _(mo):
245-
mo.md(r"""# OMEGA Data cubes""")
247+
mo.md(r"""
248+
# OMEGA Data cubes
249+
""")
246250
return
247251

248252

@@ -381,7 +385,9 @@ def _(np, omega_dc_ds):
381385

382386
@app.cell
383387
def _(mo):
384-
mo.md(r"""# Comparison between L2 and L3 products""")
388+
mo.md(r"""
389+
# Comparison between L2 and L3 products
390+
""")
385391
return
386392

387393

@@ -599,6 +605,79 @@ def _(
599605
return
600606

601607

608+
@app.cell
609+
def _(
610+
common_omega_idx,
611+
common_omega_idx_slider,
612+
img_omega,
613+
omega_c_channel_builder,
614+
):
615+
omega_c_channel_builder.make_thumbnail(
616+
common_omega_idx[common_omega_idx_slider.value],
617+
data=img_omega[1].Reflectance.values,
618+
dims=(258, 258),
619+
)
620+
return
621+
622+
623+
@app.cell
624+
def _(np, plt):
625+
from typing import Literal
626+
from PIL import Image
627+
628+
def convert_arr_to_thumbnail(
629+
data: np.ndarray,
630+
resize_dims: tuple[int, int],
631+
mode: Literal["L", "RGB", "RGBA"] = "L",
632+
cmap: str | None = None,
633+
) -> Image.Image:
634+
"""
635+
Converts a 2D or 3D NumPy array into a resized PNG-style image.
636+
Applies a matplotlib colormap if provided.
637+
"""
638+
639+
# Normalizes data between 0 and 1
640+
result = np.asarray(data, dtype=float)
641+
642+
result_min = np.nanmin(result[~np.isneginf(result)])
643+
result_max = np.nanmax(result[~np.isposinf(result)])
644+
if np.isnan(result_max) or np.isnan(result_min):
645+
raise ValueError(
646+
f"Seems like the array's size is NaN (min={result_min}, max={result_max})"
647+
)
648+
649+
result = (result - result_min) / (result_max - result_min + 1e-8)
650+
651+
if cmap is not None:
652+
cm = plt.get_cmap(cmap)
653+
result = cm(result)[..., :4] # includes alpha
654+
result = (result * 255).astype(np.uint8)
655+
if mode == "RGB":
656+
result = result[..., :3]
657+
else:
658+
result = (result * 255).astype(np.uint8)
659+
if mode in ["RGB", "RGBA"]:
660+
result = np.stack([result] * (3 if mode == "RGB" else 4), axis=-1)
661+
662+
img = Image.fromarray(result[0], mode=mode)
663+
img = img.resize(resize_dims, Image.Resampling.LANCZOS)
664+
665+
return img
666+
667+
return (convert_arr_to_thumbnail,)
668+
669+
670+
@app.cell
671+
def _(convert_arr_to_thumbnail, img_omega):
672+
convert_arr_to_thumbnail(
673+
img_omega[1].Reflectance.values,
674+
resize_dims=(258, 258),
675+
mode="RGB",
676+
cmap="viridis",
677+
)
678+
return
679+
680+
602681
@app.cell
603682
def _():
604683
return

src/psup_stac_converter/utils/file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def convert_arr_to_thumbnail(
151151
result = cm(result)[..., :4] # includes alpha
152152
result = (result * 255).astype(np.uint8)
153153
if mode == "RGB":
154-
result = result[..., :3]
154+
result = result[..., :3][0]
155155
else:
156156
result = (result * 255).astype(np.uint8)
157157
if mode in ["RGB", "RGBA"]:

0 commit comments

Comments
 (0)