Define image positioning in the QLabel
-
I wanna define image positioning in the
QLabel
, how i can calculate position?
I have aQLabel
and insert different dimension images and when i click on image it should return a color of the pixel like Color Picker should works. Now it clicks defines but with wrong pixel click.
Some people advice me useindent
andmargin
functions forQLabel
but how i don't really understand. -
Research layouts. QLabel is indirectly derived from QWidget, which supports the layout mechanism.
-
Hi,
How did you implement your color picking code ?
-
@SGaist It's main function I control mouse move and Left button click and transfer that pixel to converter to color name.
And also scale theframeCopy
convert toqimage
format and insert in grid
Also is a problem withwhile True
loop it's infinitive and should use with extra window OpenCv to terminate process by ESC buttondef main_image_color(image_path, grid_image_detect): global frame rootDirectory = os.path.dirname(__file__) #image_path = os.path.join(rootDirectory, "Media", "photo5.jpg") frame = cv.imread(image_path) height, width, _ = frame.shape bytesPerLine = 3 * width # Create a QLabel to display the image load_image = QLabel() load_image.setAlignment(Qt.AlignCenter) grid_image_detect.addWidget(load_image, 0, 2) # Connect the mousePressEvent function to the mousePressEvent signal load_image.installEventFilter(load_image) load_image.setMouseTracking(True) def eventFilter(source, event): if event.type() == QEvent.Type.MouseButtonPress and source is load_image: return mouseMoveEvent(event) return QLabel.eventFilter(load_image, source, event) load_image.eventFilter = eventFilter def mouseMoveEvent(event): global clicked_color pos = event.pos() # Get the color of the pixel at the cursor position x = int(pos.x() * width / 800) y = int(pos.y() * height / 500) if event.type() == QEvent.Type.MouseButtonPress: clicked_color = frame[y, x] print("Color at pixel", x, y, ":", clicked_color) return QLabel.mouseMoveEvent(load_image, event) load_image.mouseMoveEvent = mouseMoveEvent while True: global frameCopy # Redraw the entire image with the new text frameCopy = frame.copy() if clicked_color is not None: color_name = convert_rgb_to_names((int(clicked_color[2]), int(clicked_color[1]), int(clicked_color[0]))) text_size = cv.getTextSize(color_name, cv.FONT_HERSHEY_SIMPLEX, 2, 5)[0] # Draw a rectangle behind the text text_width, text_height = text_size[0], text_size[1] text_x = int(width/2 - text_width/2) text_y = int(height/2 + text_height/2) cv.rectangle(frameCopy, (text_x - 10, 115), (text_x + text_width + 7, 10 + text_height - 5), (255, 255, 255), -1) cv.putText(frameCopy, color_name, (text_x, 100), 0, 2, (0, 0, 0), 7) cv.putText(frameCopy, color_name, (text_x, 100), 0, 2, (int(clicked_color[0]), int(clicked_color[1]), int(clicked_color[2])), 5) q_image = QImage(frameCopy, width, height, bytesPerLine, QImage.Format_RGB888).rgbSwapped() detected_image = QPixmap.fromImage(q_image) load_image.setPixmap((detected_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio))) load_image.setAlignment(Qt.AlignmentFlag.AlignCenter) grid_image_detect.addWidget(load_image, 0, 2) cv.imshow('Project', frameCopy) cv.namedWindow("Project") cv.setMouseCallback('Project', color_detecting) key = cv.waitKey(1) if key == 27: break cv.destroyAllWindows()
-
Since you are using OpenCV to show your results as well as do the modifications on your images, why not implement your color picker directly with it ?
-
Since you are using OpenCV to show your results as well as do the modifications on your images, why not implement your color picker directly with it ?