From 7a0b54881390e0d14d4f6abe5fbb531bee1fd6ad Mon Sep 17 00:00:00 2001 From: kavyalavti Date: Sat, 14 Nov 2026 19:32:28 +0530 Subject: [PATCH] Fix image_to_url crash on non-ndarray array-like images image_to_url only passed numpy.ndarray instances to write_png. Other array-like inputs, such as nested lists, fell through to the JSON branch and crashed with AttributeError. Send every non-string image to write_png, which already accepts list-like data. Fixes #2343 Co-Authored-By: Claude Opus 5.5 --- folium/utilities.py | 2 +- tests/test_utilities.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/folium/utilities.py b/folium/utilities.py index e9807ad91d..29454de49c 100644 --- a/folium/utilities.py +++ b/folium/utilities.py @@ -195,7 +195,7 @@ def image_to_url( img = f.read() b64encoded = base64.b64encode(img).decode("utf-8") url = f"data:image/{fileformat};base64,{b64encoded}" - elif "ndarray" in image.__class__.__name__: + elif not isinstance(image, str): img = write_png(image, origin=origin, colormap=colormap) b64encoded = base64.b64encode(img).decode("utf-8") url = f"data:image/png;base64,{b64encoded}" diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 26d346ca39..21b99adc40 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -11,6 +11,7 @@ escape_double_quotes, get_obj_in_upper_tree, if_pandas_df_convert_to_numpy, + image_to_url, javascript_identifier_path_to_array_notation, normalize_bounds_type, parse_font_size, @@ -234,6 +235,25 @@ def test_is_url(url): assert _is_url(url) is True +@pytest.mark.parametrize( + "image", + [ + [[0.0, 1.0], [1.0, 0.0]], + [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 255]]], + ((0.0, 1.0), (1.0, 0.0)), + ], +) +def test_image_to_url_array_like(image): + url = image_to_url(image) + assert url == image_to_url(np.array(image)) + assert url.startswith("data:image/png;base64,") + + +def test_image_to_url_url(): + url = "https://example.com/img.png" + assert image_to_url(url) == url + + @pytest.mark.parametrize( "text,result", [