Results 1 to 2 of 2

Thread: pyqt : Draw circles and image coordinates

  1. #1
    Join Date
    Jul 2016
    Posts
    13
    Qt products
    Platforms
    Windows

    Exclamation pyqt : Draw circles and image coordinates

    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

    fvTKJ.png


    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 ):
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'test2.ui'
    4. #
    5. # Created by: PyQt4 UI code generator 4.11.4
    6. #
    7. # WARNING! All changes made in this file will be lost!
    8.  
    9. from PyQt4 import QtCore, QtGui
    10.  
    11. try:
    12. _fromUtf8 = QtCore.QString.fromUtf8
    13. except AttributeError:
    14. def _fromUtf8(s):
    15. return s
    16.  
    17. try:
    18. _encoding = QtGui.QApplication.UnicodeUTF8
    19. def _translate(context, text, disambig):
    20. return QtGui.QApplication.translate(context, text, disambig, _encoding)
    21. except AttributeError:
    22. def _translate(context, text, disambig):
    23. return QtGui.QApplication.translate(context, text, disambig)
    24.  
    25. class Ui_MainWindow(object):
    26. def setupUi(self, MainWindow):
    27. MainWindow.setObjectName(_fromUtf8("MainWindow"))
    28. MainWindow.resize(671, 507)
    29. MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    30. self.centralwidget = QtGui.QWidget(MainWindow)
    31. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    32. self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
    33. self.graphicsView.setGeometry(QtCore.QRect(20, 10, 621, 391))
    34. self.graphicsView.setMouseTracking(True)
    35. self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
    36. self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
    37. self.graphicsView.setDragMode(QtGui.QGraphicsView.NoDrag)
    38. self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
    39. self.pushButton = QtGui.QPushButton(self.centralwidget)
    40. self.pushButton.setGeometry(QtCore.QRect(70, 410, 75, 23))
    41. self.pushButton.setObjectName(_fromUtf8("pushButton"))
    42. self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
    43. self.pushButton_2.setGeometry(QtCore.QRect(270, 410, 75, 23))
    44. self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
    45. self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
    46. self.pushButton_3.setGeometry(QtCore.QRect(520, 410, 75, 23))
    47. self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
    48. MainWindow.setCentralWidget(self.centralwidget)
    49. self.menubar = QtGui.QMenuBar(MainWindow)
    50. self.menubar.setGeometry(QtCore.QRect(0, 0, 671, 21))
    51. self.menubar.setObjectName(_fromUtf8("menubar"))
    52. MainWindow.setMenuBar(self.menubar)
    53. self.statusbar = QtGui.QStatusBar(MainWindow)
    54. self.statusbar.setObjectName(_fromUtf8("statusbar"))
    55. MainWindow.setStatusBar(self.statusbar)
    56.  
    57. self.retranslateUi(MainWindow)
    58. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    59.  
    60. def retranslateUi(self, MainWindow):
    61. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    62. self.pushButton.setText(_translate("MainWindow", "Open Img", None))
    63. self.pushButton_2.setText(_translate("MainWindow", "Open", None))
    64. self.pushButton_3.setText(_translate("MainWindow", "Close", None))
    65.  
    66.  
    67. if __name__ == "__main__":
    68. import sys
    69. app = QtGui.QApplication(sys.argv)
    70. MainWindow = QtGui.QMainWindow()
    71. ui = Ui_MainWindow()
    72. ui.setupUi(MainWindow)
    73. MainWindow.show()
    74. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    tester_2 ( main code ) :
    Qt Code:
    1. import sys
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4. from test2 import *
    5.  
    6. class tester(QMainWindow, Ui_MainWindow):
    7. def __init__(self,parent=None):
    8. super(tester,self).__init__(parent)
    9. self.setupUi(self)
    10.  
    11. self.scene = QGraphicsScene()
    12. self.graphicsView.setScene(self.scene)
    13.  
    14. self.connect(self.pushButton_3,SIGNAL("clicked()") , self.close)
    15. self.connect(self.pushButton,SIGNAL("clicked()") , self.open_img)
    16.  
    17.  
    18. def open_img(self):
    19. self.filepath = QFileDialog.getOpenFileName(self,"Open",".","*jpg")
    20. self.img = QImage(self.filepath)
    21. if self.img.isNull():
    22. print("error")
    23. return
    24. self.set_pixmap()
    25.  
    26. def set_pixmap(self):
    27. self.scene.addPixmap(QPixmap.fromImage(self.img))
    28.  
    29. def draw(self):
    30. painter = QPainter()
    31. painter.begin(self.img)
    32. painter.drawEllipse(QPointF(self.x,self.y),10,10)
    33. painter.end()
    34. self.scene.addPixmap(QPixmap.fromImage(self.img))
    35.  
    36.  
    37. def mousePressEvent(self,event):
    38. self.x , self.y = event.pos().x() , event.pos().y()
    39. self.draw()
    40. self.statusbar.showMessage("%f , %f"%(self.x,self.y))
    41.  
    42. app = QApplication(sys.argv)
    43. sh = tester()
    44. sh.show()
    45. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    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 !
    Last edited by Qtdigitex; 10th July 2016 at 20:23.

  2. #2
    Join Date
    Jul 2016
    Posts
    13
    Qt products
    Platforms
    Windows

    Cool Re: pyqt : Draw circles and image coordinates

    It's Okay , i found solutions to my problems on the net

Similar Threads

  1. Replies: 2
    Last Post: 15th June 2021, 16:00
  2. draw a line between two circles
    By hichemnho in forum Newbie
    Replies: 3
    Last Post: 6th February 2012, 09:48
  3. Replies: 2
    Last Post: 27th June 2011, 13:44
  4. PyQt code to draw image from raw data?
    By StevenB in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2010, 06:09
  5. How to draw a circle with spinbox coordinates
    By rdelgado in forum Qt Programming
    Replies: 9
    Last Post: 9th August 2010, 20:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.