1

我尝试发布 QPixmap 图像 bia http。为此,我必须让 QPixmap 保存到临时文件并将其作为 python 文件类读取,然后进行 POST 工作。但我认为还有另一种发布 QPixmap 的方法。猜猜看,QPixmap 保存到 StringIO(或其他东西),用它我可以做 POST。

目前我是这样写的。

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2, os

tmpIm = "c:/tmpIm.png"
PIXMAP.save(tmpIm, "PNG")
register_openers()
_f = open(tmpIm, "rb")
datagen, headers = multipart_encode({"image": _f})
request = urllib2.Request(UPLOAD_URL, datagen, headers)
_rnt = urllib2.urlopen(request)
_f.close()
os.remove(tmpIm)
4

1 回答 1

3

您可以通过 a将 a 保存QPixmap到 a中,然后将其读入对象:QByteArrayQBufferStringIO

from PyQt4.QtCore import QBuffer, QByteArray, QIODevice
from PyQt4.QtGui import QPixmap, QApplication

import cStringIO as StringIO


if __name__ == '__main__':
    # Create a QApplication so that QPixmaps will work.
    app = QApplication([])

    # Load a PNG into a QPixmap.
    pixmap = QPixmap('c:/in.png')

    # Save QPixmap to QByteArray via QBuffer.
    byte_array = QByteArray()
    buffer = QBuffer(byte_array)
    buffer.open(QIODevice.WriteOnly)
    pixmap.save(buffer, 'PNG')

    # Read QByteArray containing PNG into a StringIO.
    string_io = StringIO.StringIO(byte_array)
    string_io.seek(0)

    # Write the StringIO back to a file to test all is ok.
    with open('c:/out.png', 'wb') as out_file:
        out_file.write(string_io.read())
于 2012-11-09T09:19:46.123 回答