Skip to content

Fix image_to_url crash on non-ndarray array-like images - #2344

Open
kavyalavti wants to merge 1 commit into
python-visualization:mainfrom
kavyalavti:fix/image-to-url-array-like
Open

kavyalavti wants to merge 1 commit into
python-visualization:mainfrom
kavyalavti:fix/image-to-url-array-like

Conversation

@kavyalavti

Copy link
Copy Markdown

Problem

ImageOverlay and folium.utilities.image_to_url document image as accepting an array-like object, but only real numpy.ndarray instances work. A nested list or tuple crashes with AttributeError: 'list' object has no attribute 'replace'.

Reproduce

import folium

m = folium.Map()
folium.raster_layers.ImageOverlay([[0.0, 1.0], [1.0, 0.0]], bounds=[[0, 0], [1, 1]]).add_to(m)

Fix

image_to_url chose the PNG branch with "ndarray" in image.__class__.__name__, so every other non-string input fell through to the JSON round-trip branch, which returns the list unchanged before .replace is called on it. The PNG branch now handles every non-string input. branca.utilities.write_png already accepts list-like data. Strings still take the same paths as before: file paths are embedded, and URLs are passed through.

Tests

  • Added test_image_to_url_array_like, which checks that lists and tuples (mono and RGB) produce the same data URL as the equivalent np.array. It fails on main and passes with the fix.
  • Added test_image_to_url_url to cover URL pass-through.
  • pytest tests --ignore=tests/selenium --ignore=tests/snapshots: 400 passed. The 3 failures in tests/test_repr.py also fail on main locally because they need a Chrome webdriver.
  • pre-commit hooks (ruff, black, codespell, etc.) and mypy pass.

AI disclosure: this change was prepared with the help of Claude Code (Anthropic). I reviewed the diff and test results.

Fixes #2343

🤖 Generated with Claude Code

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 python-visualization#2343

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageOverlay / image_to_url crash on array-like images that are not numpy arrays

1 participant