[英]pyqt : QPixmap save to StringIO?
I try to POST QPixmap image bia http. 我尝试POST QPixmap图像bia http。 To do that, I have to let QPixmap save to temporary file and read it as python file class, do POST works.
要做到这一点,我必须让QPixmap保存到临时文件并将其作为python文件类读取,做POST工作。 But I think that there is another way to POST QPixmap.
但我认为还有另一种方法来发布QPixmap。 Guess it, QPixmap save to StringIO(or something else) and with it I can do POST.
猜猜看,QPixmap保存到StringIO(或其他东西),用它我可以做POST。
Currently I wrote like that. 目前我这样写。
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)
You can save a QPixmap
to a QByteArray
via a QBuffer
and then read that into a StringIO
object: 您可以通过
QBuffer
将QPixmap
保存到QByteArray
,然后将其读入StringIO
对象:
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.