【问题标题】:PyQt image capturing errorPyQt 图像捕获错误
【发布时间】:2013-07-29 17:15:40
【问题描述】:

我正在尝试使用 PyQt 转到一组 url 并捕获那里的快照,该代码适用于一个 url:

#!/usr/bin/env python
import sys, itertools
import signal
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

urls = "http://www.gouvernement.fr/", "http://www.google.com/"
outputs = "output1.png", "output2.png"
 #for url in urls: #and output in ouputs:
for url, output in itertools.izip(urls, outputs):

    def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)

    # Set the size of the (virtual) browser window
    webpage.setViewportSize(webpage.mainFrame().contentsSize())

    # Paint this frame into an image
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    image.save(output)
    print ".png image successfully saved"
    #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(url))
    sys.exit(app.exec_())

但是当它通过一个快照时它只是挂起并且不会继续进行下一个快照。这是为什么呢?

【问题讨论】:

  • 编写的代码不会运行,因此很难判断您打算如何工作。

标签: python python-2.7 pyqt pyqt4 image-capture


【解决方案1】:

您粘贴的代码的缩进并不能清楚地表明您的 for 循环走了多远 - 我假设每次都在循环内?

如果是这样,那么很清楚为什么您的代码不起作用:您调用app.exec_,它将阻塞直到最后一个窗口关闭,或者您调用quit 或exit。就像你从来没有做的那样,它会永远阻塞。

如果您想以这种方式处理多个 url,您应该使用事件处理程序来保存页面并加载下一个 url。像这样的东西应该可以工作:

#!/usr/bin/env python
import sys, itertools
import signal
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

urls = ["http://www.gouvernement.fr/", "http://www.google.com/"]
outputs = ["output1.png", "output2.png"]

def save_page(webpage, output):
    # Set the size of the (virtual) browser window
    webpage.setViewportSize(webpage.mainFrame().contentsSize())

    # Paint this frame into an image
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    image.save(output)
    print ".png image successfully saved"
    #sys.exit(0)


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)
    save_page(webpage, outputs.pop(0))   # pop output name from list and save
    if urls:
        url = urls.pop(0)   # pop next url to fetch from list
        webpage.mainFrame().load(QUrl(url))
    else:
        app.quit()  # exit after last url

webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl(urls.pop(0)))
sys.exit(app.exec_())

【讨论】:

  • 是的,您使用缩进进行了更正,但我尝试了您的代码,它确实通过循环递归,但由于某些原因导致 python 崩溃,甚至图像也没有保存。你试过了吗?
  • 是的,我试过了。代码对我来说完全没有问题。
  • 好吧,它会生成图像,但我的 python 在台式机和笔记本电脑上仍然崩溃。我仍然得到照片。很奇怪,但谢谢!
猜你喜欢
  • 2016-03-25
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多