1

我想使用 QtWebKit (pyQt) 将网页保存为 PNG 图像。

我在互联网上找到了以下代码,但作为输出,我得到了白色图像:

import sys
import signal

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)
    # Set the size of the (virtual) browser window
    webpage.setViewportSize(QSize(640,480))

    # Paint this frame into an image
    image = QImage(QSize(640,480), QImage.Format_ARGB32)
    paint = QPainter(image)
    print webpage.mainFrame().toHtml()
    webpage.mainFrame().render(paint)
    paint.end()
    image.save("output.png")
    sys.exit(0)


app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://maps.googleapis.com/maps/api/streetview?size=640x480&location=50.073723,14.43037&pitch=-0.760&sensor=false&heading=-169.73968469794528"))

sys.exit(app.exec_())

它正确保存了我测试过的其他页面,但无法处理谷歌街景。

print webpage.mainFrame().toHtml()

显示正确的网页已加载,但不知何故它没有呈现它......你知道为什么吗?

我使用:PyQt4-4.10.2-gpl-Py2.7-Qt4.8.4-x64

4

0 回答 0