在 Python 中将 HTML 字符串转换为图像

发布于 2024-10-31 12:01:07 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2024-11-07 12:01:07

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

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

不回头走下去 2024-11-07 12:01:07

要扩展 vartec 的答案并解释如何使用它...

安装 webkit2png< /strong>
最简单的方法可能是简单地克隆 github 存储库并运行安装程序。

mkdir python-webkit2png
git clone https://github.com/adamn/python-webkit2png.git python-webkit2png
python setup.py install

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

运行它
假设您的网站位于当前目录中。 (我有一个使用 css 样式表的 html 文件 - 但无需考虑 css 文件。)

webkit2png something.html -o something.png

选项
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

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

疑难解答
使用cygwin,我遇到了webkit2png:无法连接到X服务器:0.0
为了解决这个问题(我已经执行了 export DISPLAY=0.0),我必须启动一个 X-Server。在 cygwin 上,这可以通过在第二个终端中运行 startxwin 来完成。确保首先通过 cygwin 安装程序安装它。

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

Install webkit2png
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.

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

webkit2png something.html -o something.png

Options
webkit2png -h informs us:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文