我正在尝试进行图像叠加。我正在使用卫星数据来研究阿根廷上空的现象,但我真的很想通过 folium 使用交互式 map 。到目前为止,在创建图像时,我得到了输出。但是,当我尝试将卫星图像叠加到 basemap 上时,我收到一条错误消息:
Object of type PngImageFile is not JSON serializable
我不知道如何解决它。
from PIL import Image
fig.savefig('GS.png', transparent=True)
img = Image.open("GS.png")
import folium
from folium import plugins
m = folium.Map(location=[-31.416016, -64.188929], tiles = 'Stamen Terrain')
folium.raster_layers.ImageOverlay(img,
[[ya.min(), xa.min()], [ya.max(), xa.max()]],
opacity=0.5).add_to(mapa)
mapa
最佳答案
来自 folium.raster_layers.ImageOverlay
的文档,image
参数必须是“字符串、文件或类似数组的对象”:
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.
在您的代码中,您传递了一个 PIL Image
。
img = Image.open("GS.png")
并且 Image
不可 JSON 序列化。
尝试传递图像文件的路径:
import os
img = os.path.abspath("GS.png")
folium.raster_layers.ImageOverlay(
img,
[[ya.min(), xa.min()], [ya.max(), xa.max()]],
opacity=0.5).add_to(mapa)
或者,如果您确实需要 PIL Image
,并且由于您已经有了 numpy(因为它是 folium 的依赖项),您也可以将 Image
转换为传递给 ImageOverlay 之前的 numpy 数组:
img = Image.open("GS.png")
folium.raster_layers.ImageOverlay(
numpy.array(img),
[[ya.min(), xa.min()], [ya.max(), xa.max()]],
opacity=0.5).add_to(mapa)
关于python-3.x - PngImageFile 类型的对象不可 JSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55802408/