使用 folium 在地图上重叠图像时位置不匹配

The position does not match while overlapping the image on the map using folium

此代码是草稿

bounds_lat = np.array([34.884427, 35.389037])
bounds_lon = np.array([128.761718, 129.305605])


folium.raster_layers.ImageOverlay(image=image_path,
                                  bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],                            
                                  mercator_project=True,
                                  opacity=0.3).add_to(map)

我的图像在地图中的叠加不正确,因此请添加 mercator_project=True

更改代码

bounds_lat = np.array([34.884427, 35.389037])
bounds_lon = np.array([128.761718, 129.305605])


folium.raster_layers.ImageOverlay(image=image_path,
                                  bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],
                                  mercator_project=True,
                                  opacity=0.3).add_to(map)

发生错误 请帮助我

阅读 the docs 得出以下结果:

image (string, file or array-like object) – The data you want to draw on the map. * If string, it will be written directly in the output file. * If file, it’s content will be converted as embedded in the output file. * If array-like, it will be converted to PNG base64 string and embedded in the output.

这意味着您可以对图像使用 pathimage array:

但是对于 mercator_project,文档说:

mercator_project (bool, default False.) – Used only for array-like image. Transforms the data to project (longitude, latitude) coordinates to the Mercator projection. Beware that this will only work if image is an array-like object.

因此您必须先将图像加载到 array:

from PIL import Image

image_array= Image.open(image_path)

或使用任何其他图像处理库(OpenCV 例如)。

那么你可以这样做:


folium.raster_layers.ImageOverlay(image=image_array,
                                  bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],
                                  mercator_project=True,
                                  opacity=0.3).add_to(map)