After working through this same problem I found this solution using pdfkit, wkthmltopdf, and fitz, yet without SElENIUM or anything else that requires opening a browser (I’ve been challenging myself by only coding on my IPhone/IPad with Google Colab): Convert Folium Map to PNG Without SELENIUM.
First I converted the folium map from HTML to PDF using pdfkit, then I converted the PDF to PNG using fitz & cut the image out using pillow.
Its hacky and could use a lot of improvement, but it works for me now.
More details in the Colab link above.
def convert_map_png(folium_map, file_name):
mapName = file_name
# Get HTML File of Map
folium_map.save(mapName + '.html')
htmlfile = mapName + '.html'
# Convert Map from HTML to PDF, Delay to Allow Rendering
options = {'javascript-delay': 500,
'page-size': 'Letter',
'margin-top': '0.0in',
'margin-right': '0.0in',
'margin-bottom': '0.0in',
'margin-left': '0.0in',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
]}
pdfkit.from_file(htmlfile, (mapName + '.pdf'), options=options)
pdffile = mapName + '.pdf'
# Convert Map from PDF to PNG
doc = fitz.open(pdffile)
page = doc.load_page(0)
pix = page.get_pixmap()
output = mapName + '.png'
pix.save(output)
pngfile = mapName + '.png'
doc.close()
# Crop Out Map Image
pilImage = Image.open(pngfile)
croppedImage = pilImage.crop((0,0,287,287)) # Adjust this if your map renders differently on PDF
return croppedImage