Skip to content

Fit bounds not fitting to provided points  #1877

Closed

Description

@this-josh

Describe the bug
I have a plot which I'd like to export as png so I'm using the _to_png feature. Ideally I'd like a zoom level of around 4.5 - my plot should cover from Madrid to St.Petersburg (roughly) - but I cannot provide this. When I provide bounds the image I get out doesn't seem to be obeying them.

I appreciate this is likely a function of dynamic window resizing, but exporting as png should be able to adhere to the provided bounds despite the png support being via selenium.

To Reproduce

import folium

m = folium.Map()

sw = [38, -3]
ne = [60, 31]
m.fit_bounds([sw, ne]) 

m.save("map.html")

import io
from PIL import Image

img_data = m._to_png(1)
img = Image.open(io.BytesIO(img_data))
img.save('map_plot.png')

Expected behavior
A png file contained within the provided bounds

Environment (please complete the following information):

  • Browser [e.g. chrome, firefox] Chromium based
  • Jupyter Notebook or html files? HTML and jupyer
  • Python version (check it with import sys; print(sys.version_info)) 3.12.1
  • folium version (check it with import folium; print(folium.__version__)) 0.15.1
  • branca version (check it with import branca; print(branca.__version__)) 0.7.1

Activity

  1. hansthen commented on Apr 18, 2025

    @hansthen
    Collaborator

    I can at least confirm the issue.

  2. hansthen commented on Apr 18, 2025

    @hansthen
    Collaborator

    I created a small test program

    import folium
    from streamlit_folium import st_folium
    import streamlit as st
    from selenium import webdriver
    import io
    from PIL import Image
    st.set_page_config(layout="wide")
    options = webdriver.chrome.options.Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)
    
    m = folium.Map(zoom_snap=0)
    
    sw = [38, -3]
    ne = [60, 31]
    
    folium.Rectangle(
        bounds=[sw, ne],
    ).add_to(m)
    m.fit_bounds([sw, ne])
    m.save("map.html")
    
    img_data = m._to_png(3, driver=driver)
    img = Image.open(io.BytesIO(img_data))
    
    left, right = st.columns(2)
    with left:
        st_folium(m, width=800, height=800, returned_objects=[])
    
    with right:
        st.image(img)
    
  3. hansthen commented on Apr 18, 2025

    @hansthen
    Collaborator

    The main issue seems to be the call to driver.fullscreen_window() in to_png. This is called after loading the page, which negates the set_bounds. This gives the following results

    Image

  4. hansthen commented on Apr 18, 2025

    @hansthen
    Collaborator

    If I make the driver.fullscreen_window() call before getting the image, it becomes a little better, however, the aspect ratio is still off.

    Image

  5. hansthen commented on Apr 18, 2025

    @hansthen
    Collaborator

    Replacing fullscreen_window() with set_window_size(1200,1200) makes it a little better.

    Image

    However, here it seems fit_bounds is calculated differently when using the selenium webdriver. Probably this is because the set_window_size() also includes the chromium toolbar.

  6. this-josh commented on Apr 22, 2025

    @this-josh
    ContributorAuthor

    Nice work @hansthen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions