I was wondering if it was possible to have the same event(like-R-click/R-mouse-press), mean different things for different functions :
For example : Moving an object on R-mouse press and also having the same event used for changing the shape of an object(by mouse press and drag).
My requirement : I am using a code which is used for snipping the area within, so basically it is a canvas which is being used as an overlay....
The problem i'm facing is that the overlay/snipping area never matches the image/pdf area. So currently i'm manually changing the canvas coordinates /Set_Geometry coordinates ,so that it matches the image/pdf (completely). This process takes too long. So i wanted to automate this process of changing and moving this overly /canvas by mouse. There are 2 specific operations i want :
- Being able to stretch the overlay/ canvas to match the image.
- Move this canvas around in the screen to place it exactly where the image is present.
Both of these operations i wanted to do using same the "b3-move" button. Here is the code :
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import tkinter as tk
from PIL import ImageGrab
import numpy as np
import cv2
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = screen_width/2 - dim[0]/2
y = screen_height/2 - dim[1]/2
self.setGeometry(x, y, dim[0], dim[1])
self.setWindowTitle('Snipping')
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
self.setWindowOpacity(0.8)
QtWidgets.QApplication.setOverrideCursor(
QtGui.QCursor(QtCore.Qt.CrossCursor))
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
print('Capture the screen...')
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter(self)
qp.setPen(QtGui.QPen(QtGui.QColor('black'), 3))
qp.setBrush(QtGui.QColor(128, 128, 255, 128))
qp.drawRect(QtCore.QRect(self.begin, self.end))
def mousePressEvent(self, event):
self.begin = event.pos()
self.end = self.begin
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.close()
x1 = min(self.begin.x(), self.end.x())
y1 = min(self.begin.y(), self.end.y())
x2 = max(self.begin.x(), self.end.x())
y2 = max(self.begin.y(), self.end.y())
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
cv2.imshow('Captured Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
global s
s = str(x1) +","+str(dim[1] - y1)+","+str(x2)+","+str(dim[1]- y2)
print(s)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())
How can i do this?