Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Convert HTML Image to PNG

Tags:

Given a saved HTML file with an image (an output from Bokeh), how can I save it as a PNG file with a DPI of 300?

I found some answers to similar questions, but they don't seem to work for me. I think I need someone to explain the whole process (importing the needed package, where it needs to be located if applicable, and how to call it).

I've tried this after pip installing webkit2png:

import os
os.system("webkit2png" "texas.html")

I've also tried:

import subprocess
subprocess.call("webkit2png", "texas.html")

Thanks in advance!

like image 247
Dance Party2 Avatar asked Jul 13 '16 22:07

Dance Party2


1 Answers

As of Bokeh 0.12.6, it is now possible to export PNG and SVG directly from Python code.

Exporting PNGs looks like this

export_png(plot, filename="plot.png")

And exporting SVGs looks like this

plot.output_backend = "svg"
export_svgs(plot, filename="plot.svg")

There are some optional dependencies that need to be installed. You can find more information in the Exporting Plots section of the User Guide.

Currently, plots are saved at their native resolution, but in 0.12.7 you will be able to set the size.

like image 53
Optional Argument Avatar answered Oct 03 '22 22:10

Optional Argument