In [1]:
import os
import folium

print(folium.__version__)


0.3.0.dev

How to use ImageOverlay

It may happen that you want to draw an image on you map. Here are example on how to do that.

In importing a PNG

If you have a static image file on your disk, you can simply draw it on the map in doing:


In [2]:
Mercator_projection_SW = os.path.join('data', 'Mercator_projection_SW.png')

In [3]:
from folium import plugins


m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')

plugins.ImageOverlay(
    image=open(Mercator_projection_SW, 'br'),
    bounds=[[-82, -180], [82, 180]],
    opacity=0.8,
).add_to(m)

folium.LayerControl().add_to(m)

m.save(os.path.join('results', 'ImageOverlay_0.html'))

m


Out[3]:

A few remarks:

  • In python 3, be careful to mention explictely the read mode of the file (open(..., 'rb')). You'll get an error otherwise.

You can also provide simply a file name. In this case, the image will not be embedded in folium's output : you'll need to keep it on your server when your map will be deployed.


In [4]:
Mercator_projection_SW


Out[4]:
'data/Mercator_projection_SW.png'

In [5]:
import shutil
shutil.copyfile(Mercator_projection_SW, os.path.join('results', 'merc.png'))

m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')

plugins.ImageOverlay(
    image='merc.png',
    bounds=[[-82, -180], [82, 180]],
    opacity=0.8,
).add_to(m)

folium.LayerControl().add_to(m)


m.save(os.path.join('results', 'ImageOverlay_1.html'))

m


Out[5]:

In importing a JPG

This works exactly the same way if you want to put a JPG intead of a PNG.


In [6]:
Mercator_projection_SW = os.path.join('data', 'Mercator_projection_SW.jpg')

m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')

plugins.ImageOverlay(
    image=open(Mercator_projection_SW, 'br'),
    bounds=[[-82, -180], [82, 180]],
    opacity=0.8,
).add_to(m)

folium.LayerControl().add_to(m)

m.save(os.path.join('results', 'ImageOverlay_2.html'))

m


Out[6]:

In creating an image with numpy

Now you may wish to create your own image, based on another python computation. For this, you'll need to have numpy installed on your machine.

Let's creater an image to draw a rectangle in the bounds [[0, -60], [60, 60]]


In [7]:
import numpy as np

image = np.zeros((61, 61))
image[0, :] = 1.0
image[60, :] = 1.0
image[:, 0] = 1.0
image[:, 60] = 1.0

We can draw it on the map in using:


In [8]:
m = folium.Map([37, 0], zoom_start=3)

plugins.ImageOverlay(
    image=image,
    bounds=[[0, -60], [60, 60]],
    colormap=lambda x: (1, 0, 0, x),
).add_to(m)

m


/home/filipe/miniconda3/envs/FOLIUM/lib/python3.5/site-packages/branca/utilities.py:365: RuntimeWarning: invalid value encountered in true_divide
  array = array * 255./array.max(axis=(0, 1)).reshape((1, 1, 4))
Out[8]:

Note that you need to provide a colormap of the form lambda x: (R,G,B,A) where R,G,B,A are floats between 0 and 1.

Now, let's try to add a line at latitude 45°, and add a polyline to verify it's well rendered. We'll need to specify origin='lower to inform folium that the first lines of the array are to be plotted at the bottom of the image (see numpy.imshow, it's the same principle).


In [9]:
image[45, :] = 1.0


m = folium.Map([37, 0], zoom_start=3)

plugins.ImageOverlay(
    image=image,
    bounds=[[0, -60], [60, 60]],
    colormap=lambda x: (1, 0, 0, x),
    origin='lower',
).add_to(m)

folium.PolyLine([[45, -60], [45, 60]]).add_to(m)

m.save(os.path.join('results', 'ImageOverlay_3.html'))

m


/home/filipe/miniconda3/envs/FOLIUM/lib/python3.5/site-packages/branca/utilities.py:365: RuntimeWarning: invalid value encountered in true_divide
  array = array * 255./array.max(axis=(0, 1)).reshape((1, 1, 4))
Out[9]:

But even with origin='lower', the red line is not at the good latitude. This is due to Mercator projection used in Leaflet (and most other map systems).

You can read wikipedia's article on Mercator Projection (https://en.wikipedia.org/wiki/Mercator_projection), or simply let folium do the job, in precising that you want the mercator stuff to be handled.


In [10]:
m = folium.Map([37, 0], zoom_start=3)

folium.PolyLine([[45, -60], [45, 60]]).add_to(m)

plugins.ImageOverlay(
    image=image,
    bounds=[[0, -60], [60, 60]],
    origin='lower',
    colormap=lambda x: (1, 0, 0, x),
    mercator_project=True,
).add_to(m)

m.save(os.path.join('results', 'ImageOverlay_4.html'))

m


/home/filipe/miniconda3/envs/FOLIUM/lib/python3.5/site-packages/branca/utilities.py:365: RuntimeWarning: invalid value encountered in true_divide
  array = array * 255./array.max(axis=(0, 1)).reshape((1, 1, 4))
Out[10]:

This time, the lines are properly positionned (at the precision of the array).