1

我正在尝试将 http 请求上的页面图像保存到烧瓶服务器。

这是我在运行 QObject 时得到的消息:无法为位于不同线程中的父级创建子级。

这是我的代码

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import Image
from flask import Flask, Response, jsonify,request
app=Flask(__name__)


class Screenshot(QWebView):
    def __init__(self):
        self.app = QApplication(sys.argv)
        QWebView.__init__(self)
        self._loaded = False
        self.loadFinished.connect(self._loadFinished)

    def capture(self,url,width,output_file):
        self.resize(width,300)
        self.load(QUrl(url))
        self.wait_load()
        # set to webpage size
        frame = self.page().mainFrame()
        self.page().setViewportSize(frame.contentsSize())
        # render image
        image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
        painter = QPainter(image)
        frame.render(painter)
        painter.end()
        print 'saving', output_file
        image.save(output_file)

    def wait_load(self, delay=0):
        # process app events until page loaded
        while not self._loaded:
            self.app.processEvents()
            time.sleep(delay)
        self._loaded = False

    def _loadFinished(self, result):
        self._loaded = True





if __name__=='__main__':
    s = Screenshot()

    @app.route('/image', methods=['GET','POST'])
    def getPicture():

        #reading args
        url=request.args.get('url')
        screenWidth=int(request.args.get('sw'))
        top=int(request.args.get('top'))
        left=int(request.args.get('left'))
        width=int(request.args.get('width'))
        height=int(request.args.get('height'))

        #cropping image
        s.capture(url,screenWidth,"temp.png")
        img=Image.open("temp.png")
        box=(left, top, left+width, top+height)
        area=img.crop(box)
        area.save("output","png")
        return "output.png"

        @app.route('/')
        def api_root():
            return 'Welcome'

    app.run(host='0.0.0.0',port=3000,debug=True)

每当我使用

curl http://0.0.0.0:3000/image?url=googlecom&sw=1920&top=100&left=100&width=200&height=200

我收到以下错误消息,

QObject: Cannot create children for a parent that is in a different thread.
(Parent is Screenshot(0x7f9dbf121b90), parent's thread is QThread(0x7f9dbdb92240), current thread is QThread(0x7f9dbf11ed50)
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
4

1 回答 1

0

我不确定这是否是原因,但我没有看到self.app.exec_(). Screenshot.__init__()没有它,QApplication 实例永远不会进入主循环。顺便说一句,我会在您的“主要”部分之外Screenshot但在某处实例化 QApplication 。不确定在这种特殊情况下是否重要,但可能。

于 2013-11-12T07:40:42.507 回答