Sunday, February 7, 2010

PIL to Qimage converter

import sys
from PyQt4 import QtGui
from PIL import Image
from opencv import adaptors
from opencv import cv

def get_image(w, h):
    clr = chr(0)+chr(255)+chr(0)
    pil_img = Image.open("/home/selva/Desktop/me.jpg")
    ipl_img = adaptors.PIL2Ipl(pil_img)
    #after process in opencv
    pil_img2 = adaptors.Ipl2PIL(ipl_img)
    return pil_img2

def pil2qpixmap(pil_image):
    w, h = pil_image.size
    data = pil_image.tostring("raw", "BGRX")
    qimage = QtGui.QImage(data, w, h, QtGui.QImage.Format_RGB32)
    qpixmap = QtGui.QPixmap(w,h)
    pix = QtGui.QPixmap.fromImage(qimage)
    return pix

class ImageLabel(QtGui.QLabel):
    def __init__(self, parent=None):
        QtGui.QLabel.__init__(self, parent)

        self.setGeometry(300, 300, 550, 550)
        self.setWindowTitle('Window')
               
        self.pix = pil2qpixmap(get_image(50,50))
        self.setPixmap(self.pix)



app = QtGui.QApplication(sys.argv)
ImageLabel = ImageLabel()
ImageLabel.show()
sys.exit(app.exec_())

No comments:

Post a Comment