1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| from PySide.QtGui import QApplication, QWidget, QPainter, QKeySequence,QColor
from PySide.QtGui import QImage, QRubberBand, QMainWindow, QAction, QMenuBar, QFileDialog, QScrollArea
from PySide.QtCore import QSize, QRect,QRectF
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.image = ImageView(QImage())
self.resize(500, 300)
self.setWindowTitle("Image loader")
"""
Add of scroll bars to see properly the images
"""
self.area = QScrollArea(self)
self.area.setWidget(self.image)
self.area.setWidgetResizable(True)
self.setCentralWidget(self.area)
"""
Add of the Menu which contains all the features like of Open/Save/..
and the edit options
"""
menu = QMenuBar()
self.setMenuBar(menu)
_file = menu.addMenu('File')
_edit = menu.addMenu("Edit")
# Menu Open
_action = QAction('Open', _file,shortcut=QKeySequence.Open)
_action.triggered.connect(self.__actionOpen)
_file.addAction(_action)
# Menu Save
_action = QAction('Save', _file,shortcut=QKeySequence.Save)
_action.triggered.connect(self.__actionSave)
_file.addAction(_action)
# Menu Close
_action = QAction('Close', _file,shortcut=QKeySequence.Close)
_action.triggered.connect(self.__actionClose)
_file.addAction(_action)
#Menu Edit
_action = QAction("Rubber On",_edit)
_action.triggered.connect(self.__actionRubber)
_edit.addAction(_action)
def __actionOpen(self):
_file = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if _file:
self.image.setWorkingImage(QImage(_file[0]))
else:
print "Invalid Image"
def __actionSave(self):
_file = QFileDialog.getSaveFileName(parent=None, caption="Save image as")
_result = self.image.workingImage().save(_file[0], "BMP", -1)
# Test to know if it's working
if _result:
print "Saved successfully"
else :
print "Saving failed"
def __actionClose(self):
self.close()
def __actionRubber(self):
self.__mode = "rub"
class ImageView(QWidget):
def __init__(self, image, parent=None):
super(ImageView, self).__init__(parent=parent)
self.__image = image
#Init of the boolean to know whether we select or rub
self.__mode = "rub"
#Init of selection
self.__band = QRubberBand(QRubberBand.Rectangle, self)
self.__origin = None
#Init of rubber
#the rub is a rectangle that should be the size of one pixel and the color and full
# of the one selected
self.__rub= QRectF(10.0, 20.0, 80.0, 60.0)
def setWorkingImage(self, img):
self.__image = img
self.setMinimumSize(img.size())
self.update()
def workingImage(self):
return self.__image
def mousePressEvent(self, e):
self.__origin = e.pos()
if self.__mode =="select":
self.__band.setGeometry(QRect(self.__origin, QSize()))
self.__band.show()
if self.__mode =="rub":
self.paintEvent(e)
def mouseReleaseEvent(self, e):
self.__band.hide()
print "La selection est :", QRect.intersect(self.__band.geometry(), self.rect())
def mouseMoveEvent(self, e):
self.__band.setGeometry(QRect(self.__origin, e.pos()).normalized())
def paintEvent(self, e):
painter = QPainter(self)
painter.drawImage(self.rect(), self.__image)
if self.__mode=="rub":
painter.drawLine(100,200,100,200)
if __name__ == '__main__':
app = QApplication([])
win = MainWindow()
win.show()
app.exec_() |
Partager