PDA

View Full Version : pyqt : Draw circles and image coordinates



Qtdigitex
10th July 2016, 20:15
Hello,
i'm trying to write an application to orientate scanned pictures.

the idea is to import the picture on the app , i use a QGraphicsView , the picture has 8 cross marks that i must target with the cursor

12027


and when i click on two things happens :

-i must have the clicked point coordinates in the image reference system ( origin 0,0 top left corner , x increase to right and y increase downwards ).

- to make the app more beautiful a must draw on each targeted point a circle to notify that it had been calculated , i use a QPainter for this. but it seems complicated so If i really can't , we can ignore this one

i'm a beginner and i'm having a lot of problem with this :

firstly the image has a big size : 10000x10000 so to correctly select the crossmark , i must implement a zoom function

the problem i have is that the mousePressEvent() returns the coordinates in QGraphicsView reference system wich if different from the image system.

Furthermore , mousepressEvent() works also when i click outside the QGRaphicsView Zone , wich means that the origin 0,0 is not the top left corner of the QGraphicsView.

Here's my code : my idea was to put the image on the QGRaphicsView via QGraphicsScene in order to have the same system reference Origin ( the top left corner of the image ) ..

i hope i've been clear about my problem,

with the present code when i click i get a circle but not on the right position , normally the center must be where i clicked

Test2 : ( QtDesigner Code ):

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test2.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(671, 507)
MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.Arrow Cursor))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(20, 10, 621, 391))
self.graphicsView.setMouseTracking(True)
self.graphicsView.setVerticalScrollBarPolicy(QtCor e.Qt.ScrollBarAsNeeded)
self.graphicsView.setHorizontalScrollBarPolicy(QtC ore.Qt.ScrollBarAsNeeded)
self.graphicsView.setDragMode(QtGui.QGraphicsView. NoDrag)
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(70, 410, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(270, 410, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(520, 410, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 671, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Open Img", None))
self.pushButton_2.setText(_translate("MainWindow", "Open", None))
self.pushButton_3.setText(_translate("MainWindow", "Close", None))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())



tester_2 ( main code ) :

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from test2 import *

class tester(QMainWindow, Ui_MainWindow):
def __init__(self,parent=None):
super(tester,self).__init__(parent)
self.setupUi(self)

self.scene = QGraphicsScene()
self.graphicsView.setScene(self.scene)

self.connect(self.pushButton_3,SIGNAL("clicked()") , self.close)
self.connect(self.pushButton,SIGNAL("clicked()") , self.open_img)


def open_img(self):
self.filepath = QFileDialog.getOpenFileName(self,"Open",".","*jpg")
self.img = QImage(self.filepath)
if self.img.isNull():
print("error")
return
self.set_pixmap()

def set_pixmap(self):
self.scene.addPixmap(QPixmap.fromImage(self.img))

def draw(self):
painter = QPainter()
painter.begin(self.img)
painter.drawEllipse(QPointF(self.x,self.y),10,10)
painter.end()
self.scene.addPixmap(QPixmap.fromImage(self.img))


def mousePressEvent(self,event):
self.x , self.y = event.pos().x() , event.pos().y()
self.draw()
self.statusbar.showMessage("%f , %f"%(self.x,self.y))

app = QApplication(sys.argv)
sh = tester()
sh.show()
app.exec_()


i tought about another way to do the same thing , but i'm not sure that it's more easy :

to design 3 Graphics View : the first showing the total image and inside it there will be a rectangle that we can move , the area inside this rectangle will be zoomed and putted into a second QGraphicsView with a fixed zoom ( to make it more easy ) . the second QGraphics View will too have a rectangle inside to zoom another area and put it in a third QGraphicsView with more zoom

With this method we can zoom properly the cross marks , but i still have no idea about how to get the real image pixels coordinate and not QGraphicsView coordinates with mousePressEvent() :

if there's no way to do it directly , i tought of something : if the QGraphicsView and the image inside it have the same Origin center 0,0 ( top left-corner ) and we have the clicked button inside the QGraphicsView and the real image Size ( easy to have ) , then it will be easy to calculate. But the problem as i said before , the mousePressEvent () works also outside the QgraphicsView in wich i set the MouseTracking .

i'm sorry about my english and for being long !

Qtdigitex
12th July 2016, 21:37
It's Okay , i found solutions to my problems on the net :D