Skip to content

Keep natural image size for CustomIcon without icon_size - #2334

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:customicon-natural-size
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:customicon-natural-size

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown
Contributor

Closes #1934

Problem

A CustomIcon without icon_size is shown at 25×41 px (the size of Leaflet's default marker) instead of the image's own size, so non-square logos get squashed.

Cause

folium creates the marker with Leaflet's default icon and then calls setIcon(). When both icons are images, Leaflet reuses the existing <img> element and only overwrites width, height and the margins if the new icon sets them. Without iconSize, the default icon's inline styles stay behind.

Fix

Before calling setIcon(), Marker.SetIcon clears the inline width, height, marginLeft and marginTop of the current marker element (via the public getElement()). Icons that define a size or anchor set them again as before. The cleanup uses a block-scoped const, so repeated saves still produce identical HTML (#2237).

Checked in a browser

Case Before After
CustomIcon (120×40 image), no icon_size 25×41 120×40
CustomIcon with icon_size=(60, 20) 60×20 60×20
CustomIcon with only icon_anchor 25×41 120×40, anchored
CustomIcon inside MarkerCluster 120×40 120×40
Default marker / Icon / DivIcon unchanged unchanged

Changes

  • folium/map.py: clear stale inline styles in Marker.SetIcon.
  • folium/features.py: document that the natural image size is used when icon_size is not given.
  • tests/test_map.py: test that the styles are cleared before setIcon.

Leaflet reuses the default marker's <img> when setIcon swaps in another
image icon, and keeps its inline width, height and margins when the new
icon does not set them. A CustomIcon without icon_size was therefore
shown at the default icon's 25x41 px. Clear those styles before calling
setIcon.

Closes python-visualization#1934

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
pypi — dcee8980 Deployed Nov 4, 2026 by KhadeerBasha1232 via packages #2044
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.

Keep original size of image in CustomIcon

1 participant