Skip to content

Keep integer dtype in mercator_transform - #2304

Open
ybai08 wants to merge 1 commit into
python-visualization:mainfrom
ybai08:mercator-transform-keep-dtype
Open

ybai08 wants to merge 1 commit into
python-visualization:mainfrom
ybai08:mercator-transform-keep-dtype

Conversation

@ybai08

@ybai08 ybai08 commented Oct 18, 2026

Copy link
Copy Markdown

Problem

mercator_transform always returns a float64 array. When a uint8 image is passed to ImageOverlay(..., mercator_project=True), write_png then sees float data and rescales every channel to its own maximum, so the colours change. A uniform (100, 50, 200, 255) image comes out white:

import numpy as np
import folium

data = np.full((4, 3, 4), (100, 50, 200, 255), dtype="uint8")
bounds = [[40, 0], [50, 10]]
plain = folium.raster_layers.ImageOverlay(data, bounds)
projected = folium.raster_layers.ImageOverlay(data, bounds, mercator_project=True)
# plain pixel:     (100, 50, 200, 255)
# projected pixel: (255, 255, 255, 255)

Changes

If the input has an integer dtype, round the interpolated result and cast it back to that dtype. Float input is unchanged, so the existing test_image_overlay URL stays the same.

Two regression tests were added: mercator_transform keeps uint8 data unchanged for a uniform image, and ImageOverlay produces the same URL for that image with and without mercator_project.

Validation

  • Without the fix (folium/utilities.py reverted), both new tests fail: assert dtype('float64') == <class 'numpy.uint8'> and a URL mismatch. With the fix both pass.
  • python -m pytest tests --ignore=tests/selenium --ignore=tests/snapshots: 397 passed, 3 failed. The 3 failures are in tests/test_repr.py (cannot import name 'webdriver' from 'selenium', since selenium isn't installed) and fail the same way on main.
  • ruff check folium tests (0.16.10), black --check folium tests (26.10.0), codespell on the changed files, and mypy folium pass.
  • The selenium, snapshot, and notebook suites were not run.

mercator_transform always returned a float64 array, so a uint8 image passed
to ImageOverlay(mercator_project=True) reached write_png as float data and
had every channel rescaled to its maximum. A uniform (100, 50, 200, 255)
image came out white. Round and cast the result back to the input's integer
dtype.

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.

1 participant