Python open html file, take screenshot, crop and save as image
<p>I am using the Bokeh package to generate maps to show the results of a simulation. The output is individual maps in html format with  interactivity. The interactivity is needed for individual maps. </p>

<p>See this link for an example:</p>

<p><a href="http://docs.bokeh.org/en/0.10.0/docs/gallery/texas.html" rel="nofollow noreferrer">http://docs.bokeh.org/en/0.10.0/docs/gallery/texas.html</a></p>

<p>The simulation can automatically be set to run a number of times and will produce a map for each run. This could be 100's of maps. I would like to be able to stitch together the maps to create a movie - the interactivity is not required for this. Bokeh has functionality to create PNG files via the browser so it is possible to manually save each map as a file and use ffmpeg to create a movie. However this is not really an option if you need to do it for 100's of files. Currently there is no way to automatically generate PNG files via Bokeh but I believe it will be added at some point.</p>

<p>So I need a workaround. My thought is to open each html file from the location they are stored on the local drive, take a screen shot, crop the image to keep the required section and save. But I have not yet found a solution that works.</p>

<p>Cropping an image is easy:</p>

<pre><code>from PIL import Image

img = Image.open(file_name)
box = (1, 1, 1000, 1000)
area = img.crop(box)
area.save('saved_image', 'jpeg')
</code></pre>

<p>My problem is opening the html file and taking the screen shot in the first place to feed to the above code.</p>

<p>For this I have tried the following but both require a URL rather than an html file. Also both use Firefox which doesn't work for me but I have installed chrome and altered the code appropriately.</p>

<p><a href="https://stackoverflow.com/questions/15018372/how-to-take-partial-screenshot-with-selenium-webdriver-in-python">How to take partial screenshot with Selenium WebDriver in python?</a></p>

<p><a href="http://www.idiotinside.com/2015/10/20/take-screenshot-selenium-python/" rel="nofollow noreferrer">http://www.idiotinside.com/2015/10/20/take-screenshot-selenium-python/</a></p>

<p>My code for this is:</p>

<pre><code>from selenium import webdriver

driver = webdriver.Chrome()
driver.get('file_name')
driver.save_screenshot('image.png')
driver.quit()
</code></pre>

<p>Which returns:</p>

<pre><code>{"code":-32603,"message":"Cannot navigate to invalid URL"}
</code></pre>

<p>Clearly a filename is not a url so that is clear. It works fine if you pass it a website. Any help in getting a html loaded and a picture taken would be greatly appreciated! It does not have to involve Selenium.</p>