#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import ctypes
from PyQt4 import QtGui, QtCore
import sip
'''
2009-2010 dbzhang800@gmail.com
'''
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.
label = QtGui.
QLabel(self
) self.label.setMinimumSize(512, 512)
self.comboBox.addItems(["Image.{}".format(i) for i in range(2)])
vbox.addWidget(self.label)
vbox.addWidget(self.comboBox)
self.initImages()
self.comboBox.currentIndexChanged.connect(self.onCurrentIndexChanged)
def onCurrentIndexChanged(self, index):
self.
label.
setPixmap(QtGui.
QPixmap.
fromImage(self.
images[index
]))
def initImages(self):
self.images = []
self.colorTable = [QtGui.qRgb(i, i, i) for i in range(256)]
self.createImage0()
self.createImage1()
self.
label.
setPixmap(QtGui.
QPixmap.
fromImage(self.
images[0]))
def createImage0(self):
'''Create an QImage object, the copy data from other buffer to the image buffer.
'''
image
= QtGui.
QImage(512,
512, QtGui.
QImage.
Format_Indexed8) image.setColorTable(self.colorTable)
buff = ctypes.create_string_buffer('\xFF'*512*16, 512*16)
buff2 = ctypes.create_string_buffer('\x1f'*512*32, 512*32)
img_ptr = image.bits()
ctypes.memmove(int(img_ptr), buff, buff._length_)
ctypes.memmove(int(img_ptr)+buff._length_, buff2, buff2._length_)
ctypes.memmove(int(img_ptr)+buff._length_+buff2._length_, buff, buff._length_)
self.images.append(image)
def createImage1(self):
'''we already have a buffer, create an QImage object using the buffer.
'''
self.buff = ctypes.create_string_buffer('\x7F'*512*512)
image
= QtGui.
QImage(sip.
voidptr(ctypes.
addressof(self.
buff)),
512,
512, QtGui.
QImage.
Format_Indexed8) image.setColorTable(self.colorTable)
self.images.append(image)
if __name__ == "__main__":
w = Window()
w.show()
sys.exit(app.exec_())
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import ctypes
from PyQt4 import QtGui, QtCore
import sip
'''
2009-2010 dbzhang800@gmail.com
'''
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.label = QtGui.QLabel(self)
self.label.setMinimumSize(512, 512)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItems(["Image.{}".format(i) for i in range(2)])
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(self.label)
vbox.addWidget(self.comboBox)
self.initImages()
self.comboBox.currentIndexChanged.connect(self.onCurrentIndexChanged)
def onCurrentIndexChanged(self, index):
self.label.setPixmap(QtGui.QPixmap.fromImage(self.images[index]))
def initImages(self):
self.images = []
self.colorTable = [QtGui.qRgb(i, i, i) for i in range(256)]
self.createImage0()
self.createImage1()
self.label.setPixmap(QtGui.QPixmap.fromImage(self.images[0]))
def createImage0(self):
'''Create an QImage object, the copy data from other buffer to the image buffer.
'''
image = QtGui.QImage(512, 512, QtGui.QImage.Format_Indexed8)
image.setColorTable(self.colorTable)
buff = ctypes.create_string_buffer('\xFF'*512*16, 512*16)
buff2 = ctypes.create_string_buffer('\x1f'*512*32, 512*32)
img_ptr = image.bits()
ctypes.memmove(int(img_ptr), buff, buff._length_)
ctypes.memmove(int(img_ptr)+buff._length_, buff2, buff2._length_)
ctypes.memmove(int(img_ptr)+buff._length_+buff2._length_, buff, buff._length_)
self.images.append(image)
def createImage1(self):
'''we already have a buffer, create an QImage object using the buffer.
'''
self.buff = ctypes.create_string_buffer('\x7F'*512*512)
image = QtGui.QImage(sip.voidptr(ctypes.addressof(self.buff)), 512, 512, QtGui.QImage.Format_Indexed8)
image.setColorTable(self.colorTable)
self.images.append(image)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks