19

次のHTMLをPythonでPNG画像に変換したい。

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

もちろん、このHTMLは一例です。

'pisa'を試しましたが、HTMLを画像ではなくPDFに変換します。HTMLをPDFに変換してからPDFをPNGに変換することはできますが、直接的な解決策(つまり、HTMLからPNG)があるかどうか疑問に思いました。組み込みモジュールまたは外部モジュールはすべてうまく機能します。

これがGraphicsmagickまたはImagemagickで実行できる場合は、完璧になります。

4

2 に答える 2

12

webkit2png。元のバージョンはOSXのみですが、幸いなことにクロスプラットフォームのフォークがあります: https ://github.com/AdamN/python-webkit2png

于 2011-04-12T11:00:45.557 に答える
8

vartecの回答を拡張して、その使用方法も説明します...

webkit2pngをインストール
する最も簡単な方法は、おそらくgithubリポジトリのクローンを作成してセットアップを実行することです。

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

これには、pythonとgitがすでにインストールされている必要があります。cygwinの場合、これによりwebkit2pngがコマンドとしてパスに追加されます。私は他の端末/OSでこれをテストしていません。

それを実行し
ますあなたが現在のディレクトリにあなたのウェブサイトを持っているとしましょう。(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: cannot connect to X server :0.0で、に遭遇しました。これを修正するには(私はすでに実行していましたexport DISPLAY=0.0)、X-Serverを起動する必要がありました。startxwincygwinでは、これは2番目のターミナルで実行することで実行できます。必ず最初にcygwinセットアップを介してインストールしてください。

于 2018-01-31T07:48:10.000 に答える