You can save a QPixmap
to a QByteArray
via a QBuffer
and then read that into a StringIO
object:
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())