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
|
# -*- coding: utf-8 -*-
import sys
import os
from PyQt5.QtCore import Qt, QCoreApplication
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout,
QGraphicsView, QHBoxLayout, QPushButton, QLabel,
QSpacerItem, QVBoxLayout, QSizePolicy, QFileDialog,
QGraphicsScene)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.setWindowTitle("Main Window")
self.widget = QWidget(self)
self.gLayout = QGridLayout(self.widget)
self.vLayout = QVBoxLayout()
self.viewer = QGraphicsView(self.widget)
self.vLayout.addWidget(self.viewer)
self.hLayout = QHBoxLayout()
self.image_btn = QPushButton(self.widget)
self.image_btn.setText("Image")
self.hLayout.addWidget(self.image_btn)
self.xml_btn = QPushButton(self.widget)
self.xml_btn.setText("Update xml")
self.hLayout.addWidget(self.xml_btn)
self.points_lbl = QLabel(self.widget)
self.points_lbl.setText("Coordonnées point:")
self.hLayout.addWidget(self.points_lbl)
spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.hLayout.addItem(spacer)
self.close_btn = QPushButton(self.widget)
self.close_btn.setText("Close")
self.hLayout.addWidget(self.close_btn)
self.vLayout.addLayout(self.hLayout)
self.gLayout.addLayout(self.vLayout, 0, 0, 1, 1)
self.setCentralWidget(self.widget)
self.scene = QGraphicsScene()
self.viewer.setScene(self.scene)
self.image_btn.clicked.connect(self.open_image_file)
self.xml_btn.clicked.connect(self.write_xml)
self.close_btn.clicked.connect(self.closeEvent)
self.scene.mousePressEvent = self.on_image_clicked
def open_image_file(self):
fname = QFileDialog.getOpenFileName(self, "Ouvrir un fichier image",
os.path.expanduser("~"),
"Images (*.png *.jpg *.jpeg)")[0]
if os.path.isfile(fname):
self.display_image(fname)
def write_xml(self):
print('Update xml file\n Image: %s\n Coord: %s' %(self.image_path,
str(self.points)))
def closeEvent(self, event=None):
QCoreApplication.processEvents()
self.close()
def display_image(self, path):
self.scene.clear()
self.points = []
self.image_path = path
w, h = self.viewer.width(), self.viewer.height()
qimage = QImage(path)
self.pixmap = QPixmap.fromImage(qimage.scaled(w, h,
Qt.KeepAspectRatio,
Qt.SmoothTransformation))
pw, ph = self.pixmap.width(), self.pixmap.height()
self.scene.setSceneRect(0, 0, pw, ph)
self.scene.addPixmap(self.pixmap)
self.zoom_factor = qimage.width() / pw
def on_image_clicked(self, event):
pos = event.scenePos()
if 0 < pos.x() < self.pixmap.width() and \
0 < pos.y() < self.pixmap.height():
self.points_lbl.setText("Coordonnées point: %s, %s"
%(pos.x(), pos.y()))
self.points.append((pos.x()*self.zoom_factor,
pos.y()*self.zoom_factor))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_()) |
Partager