To convert from python string to QString:
Qt_string = QtCore.QString(python_string)
To convert from QString to python string:
python_string = str(Qt_string)
Thursday, February 18, 2010
Saturday, February 13, 2010
QPixmap, QBitmap and QImage
Setting rectangle to choose crop area
def mousePressEvent(self, event):
self.rbOrigin = self.mapFromGlobal(event.globalPos())
self.rubberBand.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
self.rubberBand.show()
self.x1 = int(QtCore.QPoint.x(self.rbOrigin))
self.y1 = int(QtCore.QPoint.y(self.rbOrigin))
print self.x1
print self.y1
def mouseMoveEvent ( self, event ) :
self.x2 = int(QtCore.QPoint.x(self.mapFromGlobal(event.globalPos())))
self.y2 = int(QtCore.QPoint.y(self.mapFromGlobal(event.globalPos())))
self.x = self.x2 - self.x1
self.y = self.y2 - self.y1
if (self.x == 0):
self.x = 4*self.y
else:
self.y = self.x/4
self.x2 = self.x + self.x1
self.y2 = self.y + self.y1
self.rubberBand.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, self.y1), QtCore.QPoint(self.x2, self.y2)).normalized())
self.rbOrigin = self.mapFromGlobal(event.globalPos())
self.rubberBand.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
self.rubberBand.show()
self.x1 = int(QtCore.QPoint.x(self.rbOrigin))
self.y1 = int(QtCore.QPoint.y(self.rbOrigin))
print self.x1
print self.y1
def mouseMoveEvent ( self, event ) :
self.x2 = int(QtCore.QPoint.x(self.mapFromGlobal(event.globalPos())))
self.y2 = int(QtCore.QPoint.y(self.mapFromGlobal(event.globalPos())))
self.x = self.x2 - self.x1
self.y = self.y2 - self.y1
if (self.x == 0):
self.x = 4*self.y
else:
self.y = self.x/4
self.x2 = self.x + self.x1
self.y2 = self.y + self.y1
self.rubberBand.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, self.y1), QtCore.QPoint(self.x2, self.y2)).normalized())
Tuesday, February 9, 2010
Converting from qimage to pil image
buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.ReadWrite)
self.qimage.save(buffer, "JPG")
strio = cStringIO.StringIO()
strio.write(buffer.data())
buffer.close()
strio.seek(0)
pil_img = Image.open(strio)
buffer.open(QtCore.QIODevice.ReadWrite)
self.qimage.save(buffer, "JPG")
strio = cStringIO.StringIO()
strio.write(buffer.data())
buffer.close()
strio.seek(0)
pil_img = Image.open(strio)
Sunday, February 7, 2010
Resize image in opencv
from opencv.cv import *
from opencv.highgui import *
if __name__ == '__main__':
ipl = cvLoadImage("/home/selva/Desktop/me.jpg");
newImage = cvCreateImage(cvSize(48, 16), ipl.depth, ipl.nChannels);
cvResize(ipl, newImage, CV_INTER_LINEAR);
cvNamedWindow("image pop", CV_WINDOW_AUTOSIZE)
cvShowImage("image pop",newImage)
cvWaitKey()
from opencv.highgui import *
if __name__ == '__main__':
ipl = cvLoadImage("/home/selva/Desktop/me.jpg");
newImage = cvCreateImage(cvSize(48, 16), ipl.depth, ipl.nChannels);
cvResize(ipl, newImage, CV_INTER_LINEAR);
cvNamedWindow("image pop", CV_WINDOW_AUTOSIZE)
cvShowImage("image pop",newImage)
cvWaitKey()
crop image in opencv
from opencv.cv import *
from opencv.highgui import *
if __name__ == '__main__':
new_width = 120
new_height = 110
ipl = cvLoadImage("/home/selva/Desktop/me.jpg");
cropped = cvCreateImage( cvSize(new_width, new_height), 8, 3)
src_region = cvGetSubRect(ipl, cvRect(110, 120, new_width, new_height) )
cvCopy(src_region, cropped)
cvNamedWindow("image pop", CV_WINDOW_AUTOSIZE)
cvShowImage("image pop",cropped)
cvWaitKey()
from opencv.highgui import *
if __name__ == '__main__':
new_width = 120
new_height = 110
ipl = cvLoadImage("/home/selva/Desktop/me.jpg");
cropped = cvCreateImage( cvSize(new_width, new_height), 8, 3)
src_region = cvGetSubRect(ipl, cvRect(110, 120, new_width, new_height) )
cvCopy(src_region, cropped)
cvNamedWindow("image pop", CV_WINDOW_AUTOSIZE)
cvShowImage("image pop",cropped)
cvWaitKey()
Opening Qt loaded file in PIL
fileName = QtGui.QFileDialog.getOpenFileName(self, self.tr("Open File"),
QtCore.QDir.currentPath())
pil_img = self.setImage(str(fileName))
def setImage(self, filename):
"set image to given filename"
self.filename = filename
self.pilImg = None
if not (filename and os.path.exists(filename)):
return
try:
pilImg = Image.open(filename)
except IOError:
self.writeStatus("Error loading image")
return
return pilImg
QtCore.QDir.currentPath())
pil_img = self.setImage(str(fileName))
def setImage(self, filename):
"set image to given filename"
self.filename = filename
self.pilImg = None
if not (filename and os.path.exists(filename)):
return
try:
pilImg = Image.open(filename)
except IOError:
self.writeStatus("Error loading image")
return
return pilImg
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_())
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_())
PIL to IPL and IPL to PIL converters
# use OpenCV-2.0.0 and later
from opencv.cv import *
from opencv import adaptors
from opencv.highgui import *
from PIL import Image
if __name__ == '__main__':
pil_img = Image.open('/home/selva/Desktop/me.jpg')
cv_img = adaptors.PIL2Ipl(pil_img)
cvNamedWindow("pil2ipl")
cvShowImage("pil2ipl", cv_img)
cvWaitKey()
from opencv.cv import *
from opencv import adaptors
from opencv.highgui import *
from PIL import Image
if __name__ == '__main__':
pil_img = Image.open('/home/selva/Desktop/me.jpg')
cv_img = adaptors.PIL2Ipl(pil_img)
cvNamedWindow("pil2ipl")
cvShowImage("pil2ipl", cv_img)
cvWaitKey()
Subscribe to:
Posts (Atom)