I want to convert following HTML to PNG image in Python.
<html> <b>Bold text</b> </html>
This HTML is, of course, an example.
I have tried ‘pisa’ but it converts html to PDF, not to image.
I can convert HTML to PDF and then convert PDF to PNG, but I was wondering if there is any direct solution (i.e HTML to PNG). Any built-in or external module will work nicely.
If this can be done in Graphicsmagick or Imagemagick, then it will be perfect.
webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork:
To expand on vartec’s answer to also explain how to use it…
The easiest way is probably to simply clone the github repo and run the setup.
mkdir python-webkit2png git clone https://github.com/adamn/python-webkit2png.git python-webkit2png python setup.py install
This requires python and git to already be installed.
For cygwin, this will add webkit2png as a command to the path. I haven’t tested this for other terminals/OS.
Say you have your website in the current directory. (I had a html file that was using a css stylesheet – but there’s no need to think about the css file.)
webkit2png something.html -o something.png
webkit2png -h informs us:
Notable options are the setting of width and height.
Using cygwin, I encountered
webkit2png: cannot connect to X server :0.0.
To fix this (I had already performed
export DISPLAY=0.0), I had to start an X-Server. On cygwin, this can be done by running
startxwin in a second terminal. Make sure to install it first via the cygwin setup.