Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ImageQt usage with PySide

By: reclosedev on Mar 21st, 2012  |  syntax: Python  |  size: 1.55 KB  |  views: 237  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4.  
  5. import PySide
  6. sys.modules['PyQt4'] = PySide # HACK for ImageQt
  7.  
  8. from PySide import QtCore, QtGui
  9. import Image
  10. import ImageQt
  11. import ImageEnhance
  12. import time
  13.  
  14. class TestWidget(QtGui.QWidget):
  15.     def __init__(self, parent=None):
  16.         QtGui.QWidget.__init__(self, parent)
  17.         self.scene = QtGui.QGraphicsScene()
  18.         self.view = QtGui.QGraphicsView(self.scene)
  19.         self.button = QtGui.QPushButton("Do test")
  20.  
  21.         layout = QtGui.QVBoxLayout()
  22.         layout.addWidget(self.button)
  23.         layout.addWidget(self.view)
  24.         self.setLayout(layout)
  25.  
  26.         self.button.clicked.connect(self.do_test)
  27.  
  28.     def do_test(self):
  29.         img = Image.open('image.png')
  30.         enhancer = ImageEnhance.Brightness(img)
  31.         for i in range(1, 8):
  32.             img = enhancer.enhance(i)
  33.             self.display_image(img)
  34.             QtCore.QCoreApplication.processEvents()  # let Qt do his work
  35.             time.sleep(0.5)
  36.  
  37.     def display_image(self, img):
  38.         self.scene.clear()
  39.         w, h = img.size
  40.         self.imgQ = ImageQt.ImageQt(img)  # we need to hold reference to imgQ, or it will crash
  41.         pixMap = QtGui.QPixmap.fromImage(self.imgQ)
  42.         self.scene.addPixmap(pixMap)
  43.         self.view.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
  44.         self.scene.update()
  45.  
  46. if __name__ == "__main__":
  47.     app = QtGui.QApplication(sys.argv)
  48.     widget = TestWidget()
  49.     widget.resize(640, 480)
  50.     widget.show()
  51.  
  52.     sys.exit(app.exec_())