Search code examples
pythonfolium

Add text to Folium map using an absolute position


I have defined in my Python program:

fig = Figure(width, height)
map = folium.Map(location=[y, x], zoom_start=2)
fig.add_child(map)

How can I add text to my map using absolute position (not latitude/longitude one) ? A position defined by a percentage of the width and the height of the Figure. Something like

Text("Toto is my name", pos_x=0.1*width,pos_y=0.05*height)

Solution

  • here my solution. FloatImage do the job for an image ... so I have decided to convert my text into png and then use this method

    from PIL import Image, ImageDraw, ImageFont
    W, H = (300,200)
    im = Image.new("RGBA",(W,H))
    draw = ImageDraw.Draw(im)
    msg = "pycoa.fr (data from: {})".format(mypandas.data_base)
    w, h = draw.textsize(msg)
    fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 14)
    draw.text((0,0), msg, font=fnt,fill=(0, 0, 0))
    im.crop((0, 0,2*w,2*h)).save("pycoatextlogo.png", "PNG")
    FloatImage("pycoatextlogo.png", bottom=0, left=0).add_to(map)
    

    It is not perfect, but it works :) enter image description here