0

我想在完全加载时将页面内容保存到图像中,但有时我得到的输出栅格未完全渲染。

代码:

import sys
import signal
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)
    webpage.setViewportSize(webpage.mainFrame().contentsSize())
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    if os.path.exists("output.png"):
        os.remove("output.png")
    image.save("output.png")
    sys.exit(0) # quit this application

webpage.mainFrame().load(QUrl("file:///page.html"))
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
sys.exit(app.exec_())

页面正在使用 JavaScript (onload function) 获取 google map (640x640px) 。

图片:http: //i56.tinypic.com/15ojg3s.png

4

1 回答 1

1

我不确定这是否可能。对于静态网站,这可能可行,但谷歌地图会动态加载图块,我怀疑它会发出一个有用的“我完成了”信号。

但您似乎只想要一张谷歌地图的图像?你看过他们的 API 吗?它们允许您生成静态地图,只需构建一个 URL。

例子

http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap &markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318 &markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false

示例地图

于 2011-02-11T13:54:47.270 回答