Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: account for 0 width arrays (#8950)

Dylan Madisetti036cc72ad3bb
diff --git a/marimo/_plugins/stateless/image.py b/marimo/_plugins/stateless/image.py
index 84fa59755f..9942fa0679 100644
--- a/marimo/_plugins/stateless/image.py
+++ b/marimo/_plugins/stateless/image.py
@@ -82,6 +82,12 @@ def _normalize_image(
is_uint8 = src.__array_interface__["typestr"] == "|u1"
has_bounds = vmin is not None or vmax is not None
+ if 0 in src.__array_interface__.get("shape", (0,)):
+ raise ValueError(
+ f"Cannot render an image from an array with a zero-size "
+ f"dimension (shape {src.__array_interface__['shape']!r})."
+ )
+
if not is_uint8 or has_bounds:
lo = float(vmin) if vmin is not None else float(src.min())
hi = float(vmax) if vmax is not None else float(src.max())
diff --git a/tests/_plugins/stateless/test_image.py b/tests/_plugins/stateless/test_image.py
index 44199b5351..9506f0b287 100644
--- a/tests/_plugins/stateless/test_image.py
+++ b/tests/_plugins/stateless/test_image.py
@@ -211,6 +211,14 @@ def test_image_constructor_pil():
assert result.text.startswith("<img src='data:image/png;base64,")
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_image_zero_width_array():
+ import numpy as np
+
+ with pytest.raises(ValueError, match="zero-size dimension"):
+ image(np.zeros((100, 0, 3)))
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
def test_image_uint8_no_normalization():
import numpy as np