I am trying to save a map containing markers and also heatmap into an image.
Here is the code to display the map.
from ipywidgets import Layout
import geopandas
defaultLayout=Layout(width='3000px', height='3000px') # A very large image.
lat_lgn = [39.74248, 254.993622]
m_f = Map(center=lat_lgn, zoom=12, layout=defaultLayout)
marker_m = Marker(location=lat_lgn, draggable=False)
m_f.add_layer(marker_m)
m_f
Add some markers on it
arr_test1 = [39.74258, 254.993682]
arr_test2 = [39.76288, 254.988932]
arr_test3 = [39.79998, 254.991982]
all_loc = [arr_test1, arr_test2, arr_test3]
for cur_loc in all_loc:
point = CircleMarker(
radius=10,
location=cur_loc,
color='red',
fill_color="black",
)
m_f.add_layer(point)
time.sleep(0.001)
Save the map as html file. Works fine.
m_f.save('f_map.html', title='My Map')
The problem occurs, when I try to get an image, or pdf from the html.
import imgkit
import pdfkit
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_file('f_map.html', 'outpdf.pdf', configuration=config)
pdfkit.from_file('f_map.html', 'outjpg.jpg', configuration=config)
pdfkit.from_file('f_map.html', 'outpng.png', configuration=config)
The pdf file is blank
And macBook is not able the open neither the jpeg nor the png file.
To ckeck my dependencies, I have tried this:
import pdfkit
pdfkit.from_url('http://stackoverflow.com', 'out.pdf', configuration=config)
which works fine. However, once I change out.pdf to out.png, I cannot open the obtained file.
Does anyone has an idea, how I can solve the issue ?
I am trying to get hurge image. But it also did not work with a 300px X 300px image.
Any hints will be welcome.
One option (which admittedly is a little crude) is to use the selenium library to automatically open the HTML and from there take a screenshot which then is saved as an image.
The code will look like (I have given the code here according to MS Edge, but the code should be similar for other browsers):
from selenium import webdriver
from time import sleep
filename = '<Name (without path) of your HTML file here>'
MyBrowser = webdriver.Edge(r"<path to webdriver here>\\msedge.exe")
murl = r'file:\\{path}\{mapfile}'.format(path='<path to HTML here>',mapfile=mfilename)
MyBrowser.get(murl)
sleep(10) #this is not strictly necessary but is here in case you need time for map tiles to load
MyBrowser.save_screenshot('<Image_file_name>')
MyBrowser.quit()
Note that this will require you to install a webdriver for your browser in advance.
Related
I am using ipyvuetify to code an app to be rendered with voila and I would like to use an image as icon for footer (or in the future for a button). Any idea how to do it?
This is the code for an icon
v.Footer( absolute = False,
class_="font-weight-medium",
children= [v.Col(class_="text-center", cols="12", children=[v.Icon(children=['fingerprint']),'BMW - 2020 - alpha version 0.0. powered by Soft company PPP'])]
this will generate:
I want to use my own logo instead of the predefined fingerprint.
So how can I load an image and give a relative size to the font.
thanks
With a few modifications to Christoph Weiss-Schabers answer it can be done with ipyvuetify:
import base64
import ipyvuetify as v
file = open( 'LINK_TO_YOUR_ICON', 'rb')
image = file.read()
image_base64 = base64.b64encode(image).decode('ascii')
img = v.Img(src=f'data:image/png;base64,{image_base64}')
v.Footer( absolute = False,
class_="font-weight-medium",
children= [v.Col(class_="text-center", cols="12", children=[img,'BMW - 2020 - alpha version 0.0. powered by Soft company PPP'])]
)
Or for an online image:
v.Img(src='https://homepages.cae.wisc.edu/~ece533/images/fruits.png', width='100', height='100')
Currently there seems to be no good way to simply pass relative link to ipyvuetify and have it display the image (if there).
A workaround I found is to open the file with ipywidgets and pass this to ipyvuetify as object:
import ipywidgets as widgets
file = open( 'LINK_TO_YOUR_ICON', 'rb')
image = file.read()
img = widgets.Image(value=image, format='png')
v.Footer( absolute = False,
class_="font-weight-medium",
children= [v.Col(class_="text-center", cols="12", children=[img,'BMW - 2020 - alpha version 0.0. powered by Soft company PPP'])]
)
check if this solves your problem ;)
I am trying to use colab and googlemaps to display a heatmap image overlayed onto google maps. I did some research and found 2 possible solutions at this link but both solutions are not working.
Display / Render an HTML file inside Jupyter Notebook on Google Colab platform
Maybe something has changed recently?
Here is my code:
pip install gmplot
import gmplot
gmap1 = gmplot.GoogleMapPlotter(30.3164945,
78.03219179999999, 13, apikey='AIzaSyBCyhpxDYIxIq9uINYxDK5aIjWSokUvsvY' )
gmap1.draw( "map11.html" )
import os
print(os.getcwd())
!ls
/content
map11.html sample_data
import IPython
IPython.display.HTML(filename='/content/map11.html')
nothing is displayed.
from IPython.display import IFrame
IFrame(src='/content/map11.html', width=900, height=600)
localhost refused to connect.
Your local browser cannot read a file from the remote VM filesystem. Instead of passing the HTML by filename, try displaying the HTML content directly:
IPython.display.HTML(open('/content/map11.html').read())
I have a very small application written in PyCharm using python3:
import folium
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save('map2.html')
This will create a map2.html which I can access in my browser by using pycharm and the url looks like: http://localhost:63342/iss-country/map2.html?_ijt=dcsefdg8om4ddfovlt5ooq6ro5
How can I automatically open this in my browser? So when I run the application it does not only generate the html page but also visits it immediatley. I found the webbrowser module which can be useful but how do I know the correct localhost url?
I don't see the issue with using the webbrowser module. Just make the file name and path a variable and call the webbrowser open method.
output_file = "map2.html"
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save(output_file)
webbrowser.open(output_file, new=2) # open in new tab
When I click the Download as markdown in the menu,
it will give me zip file with .md, .png, '.svg'
Note: I use set_matplotlib_formats('png', 'svg'), so the download file returns 2 image file formats.
Now, I want to save all the images in this notebook into svg by program (i.e., writing Python script), how can I do it? Maybe something like
save_images_to_svg('this_notebook.ipynb')
# and it will save all images to '.\images\*.svg'
What I know so far:
The link for Download as markdown is at here
this.element.find('#download_markdown').click(function () {
that._nbconvert('markdown', true);
});
It further binds to the _nbconvert at here
MenuBar.prototype._nbconvert = function (format, download) {
download = download || false;
var notebook_path = utils.encode_uri_components(this.notebook.notebook_path);
var url = utils.url_path_join(
this.base_url,
'nbconvert',
format,
notebook_path
) + "?download=" + download.toString();
this._new_window(url);
};
This means that the notebook sends a request to the backend to do the conversion.
I don't know where it is, but it seems like here, because it has a respond_zip method.
I think a quick hack is simply to download the file using python with the url from MenuBar.prototype._nbconvert.
Create the plot using matplotlib and save the image with the following command. It works in Sage 8.1 (windows). You will find the figures where your .ipynb file is located.
import numpy
import matplotlib.pyplot as plt
x = numpy.arange(-2 * numpy.pi, 2 * numpy.pi, 0.1)
func = numpy.sin(x)
plt.figure(figsize=(3.3, 3.3)) # inches
plt.plot(x, func, linewidth=2.0, color='blue')
plt.savefig('demo1.svg')
plt.savefig('demo1.pdf')
plt.savefig('demo1.eps')
plt.savefig('demo1.tif')
plt.show()
# plt.close()
I am trying to reload an image in a loop from the blob's URL into a text area.
So, I am following the given IronPython examples to be able to download a blob and set to a document property called Img1 and eventually I'm setting that property to a label inside the text area. And my plan is to run this in a wait-loop cycle.
The code looks like this:
#from System.Drawing import Image
from System import Uri
from System.Net import HttpWebRequest
from Spotfire.Dxp.Data import BinaryLargeObject
uri = Uri("http://100.206.214.99/remote/display.bmp")
request = HttpWebRequest.Create(uri)
response = request.GetResponse()
stream = response.GetResponseStream()
blob = BinaryLargeObject.Create(stream)
Document.Properties["Img1"] = blob
It's all working fine, but I'm not able to adjust the height or width of the image/blob. Can anyone help me with this?