在Python中将HTML转换为图像

时间:2022-03-24 00:18:05

I want to convert following HTML to PNG image in Python.

我想在Python中将以下HTML转换为PNG图像。

<html>
    <b>Bold text</b>
</html>

This HTML is, of course, an example.

当然,这个HTML就是一个例子。

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.

我尝试了'pisa',但它将html转换为PDF,而不是图像。我可以将HTML转换为PDF然后将PDF转换为PNG,但我想知道是否有任何直接的解决方案(即HTML到PNG)。任何内置或外置模块都可以很好地工作。

If this can be done in Graphicsmagick or Imagemagick, then it will be perfect.

如果这可以在Graphicsmagick或Imagemagick中完成,那么它将是完美的。

3 个解决方案

#1


7  

webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork: https://github.com/AdamN/python-webkit2png

webkit2png。原始版本仅限OSX,但幸运的是有一个跨平台的分支:https://github.com/AdamN/python-webkit2png

#2


1  

Another possible solution is to use GrabzIt's free HTML to Image API for Python. You could then convert your HTML to PDF code like this:

另一种可能的解决方案是使用GrabzIt的免费HTML to Image API for Python。然后,您可以将HTML转换为PDF代码,如下所示:

import GrabzItClient
grabzIt = GrabzItClient.GrabzItClient("APPLICATION KEY", "APPLICATION SECRET")
grabzIt.HTMLToImage("<html><b>Bold text</b></html>")    
grabzIt.SaveTo("test.png")

Full disclosure I am the API creator.

完全披露我是API创建者。

#3


1  

To expand on vartec's answer to also explain how to use it...

扩展vartec的答案也解释了如何使用它...

Install webkit2png
The easiest way is probably to simply clone the github repo and run the setup.

安装webkit2png最简单的方法可能是简单地克隆github repo并运行安装程序。

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.

这需要已经安装了python和git。对于cygwin,这将添加webkit2png作为路径的命令。我还没有测试过其他终端/操作系统。

Run it
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.)

运行它说您的网站在当前目录中。 (我有一个使用css样式表的html文件 - 但是没有必要考虑css文件。)

webkit2png something.html -o something.png

Options
webkit2png -h informs us:

选项webkit2png -h通知我们:

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -x WIDTH HEIGHT, --xvfb=WIDTH HEIGHT
                        Start an 'xvfb' instance with the given desktop size.
  -g WIDTH HEIGHT, --geometry=WIDTH HEIGHT
                        Geometry of the virtual browser window (0 means
                        'autodetect') [default: (0, 0)].
  -o FILE, --output=FILE
                        Write output to FILE instead of STDOUT.
  -f FORMAT, --format=FORMAT
                        Output image format [default: png]
  --scale=WIDTH HEIGHT  Scale the image to this size
  --aspect-ratio=RATIO  One of 'ignore', 'keep', 'crop' or 'expand' [default:
                        none]
  -F FEATURE, --feature=FEATURE
                        Enable additional Webkit features ('javascript',
                        'plugins')
  -c COOKIE, --cookie=COOKIE
                        Add this cookie. Use multiple times for more cookies.
                        Specification is value of a Set-Cookie HTTP response
                        header.
  -w SECONDS, --wait=SECONDS
                        Time to wait after loading before the screenshot is
                        taken [default: 0]
  -t SECONDS, --timeout=SECONDS
                        Time before the request will be canceled [default: 0]
  -W, --window          Grab whole window instead of frame (may be required
                        for plugins)
  -T, --transparent     Render output on a transparent background (Be sure to
                        have a transparent background defined in the html)
  --style=STYLE         Change the Qt look and feel to STYLE (e.G. 'windows').
  --encoded-url         Treat URL as url-encoded
  -d DISPLAY, --display=DISPLAY
                        Connect to X server at DISPLAY.
  --debug               Show debugging information.
  --log=LOGFILE         Select the log output file

Notable options are the setting of width and height.

值得注意的选项是宽度和高度的设置。

Troubleshooting
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.

故障排除使用cygwin,我遇到webkit2png:无法连接到X服务器:0.0。要解决此问题(我已经执行了导出DISPLAY = 0.0),我必须启动一个X-Server。在cygwin上,这可以通过在第二个终端中运行startxwin来完成。确保首先通过cygwin设置安装它。

#1


7  

webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork: https://github.com/AdamN/python-webkit2png

webkit2png。原始版本仅限OSX,但幸运的是有一个跨平台的分支:https://github.com/AdamN/python-webkit2png

#2


1  

Another possible solution is to use GrabzIt's free HTML to Image API for Python. You could then convert your HTML to PDF code like this:

另一种可能的解决方案是使用GrabzIt的免费HTML to Image API for Python。然后,您可以将HTML转换为PDF代码,如下所示:

import GrabzItClient
grabzIt = GrabzItClient.GrabzItClient("APPLICATION KEY", "APPLICATION SECRET")
grabzIt.HTMLToImage("<html><b>Bold text</b></html>")    
grabzIt.SaveTo("test.png")

Full disclosure I am the API creator.

完全披露我是API创建者。

#3


1  

To expand on vartec's answer to also explain how to use it...

扩展vartec的答案也解释了如何使用它...

Install webkit2png
The easiest way is probably to simply clone the github repo and run the setup.

安装webkit2png最简单的方法可能是简单地克隆github repo并运行安装程序。

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.

这需要已经安装了python和git。对于cygwin,这将添加webkit2png作为路径的命令。我还没有测试过其他终端/操作系统。

Run it
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.)

运行它说您的网站在当前目录中。 (我有一个使用css样式表的html文件 - 但是没有必要考虑css文件。)

webkit2png something.html -o something.png

Options
webkit2png -h informs us:

选项webkit2png -h通知我们:

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -x WIDTH HEIGHT, --xvfb=WIDTH HEIGHT
                        Start an 'xvfb' instance with the given desktop size.
  -g WIDTH HEIGHT, --geometry=WIDTH HEIGHT
                        Geometry of the virtual browser window (0 means
                        'autodetect') [default: (0, 0)].
  -o FILE, --output=FILE
                        Write output to FILE instead of STDOUT.
  -f FORMAT, --format=FORMAT
                        Output image format [default: png]
  --scale=WIDTH HEIGHT  Scale the image to this size
  --aspect-ratio=RATIO  One of 'ignore', 'keep', 'crop' or 'expand' [default:
                        none]
  -F FEATURE, --feature=FEATURE
                        Enable additional Webkit features ('javascript',
                        'plugins')
  -c COOKIE, --cookie=COOKIE
                        Add this cookie. Use multiple times for more cookies.
                        Specification is value of a Set-Cookie HTTP response
                        header.
  -w SECONDS, --wait=SECONDS
                        Time to wait after loading before the screenshot is
                        taken [default: 0]
  -t SECONDS, --timeout=SECONDS
                        Time before the request will be canceled [default: 0]
  -W, --window          Grab whole window instead of frame (may be required
                        for plugins)
  -T, --transparent     Render output on a transparent background (Be sure to
                        have a transparent background defined in the html)
  --style=STYLE         Change the Qt look and feel to STYLE (e.G. 'windows').
  --encoded-url         Treat URL as url-encoded
  -d DISPLAY, --display=DISPLAY
                        Connect to X server at DISPLAY.
  --debug               Show debugging information.
  --log=LOGFILE         Select the log output file

Notable options are the setting of width and height.

值得注意的选项是宽度和高度的设置。

Troubleshooting
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.

故障排除使用cygwin,我遇到webkit2png:无法连接到X服务器:0.0。要解决此问题(我已经执行了导出DISPLAY = 0.0),我必须启动一个X-Server。在cygwin上,这可以通过在第二个终端中运行startxwin来完成。确保首先通过cygwin设置安装它。