Получение положения мыши относительно границ изображения

#python #pyqt #pyqt5 #qpixmap #qmouseevent

Вопрос:

Я показываю изображение в пользовательском QLabel файле и получаю щелчки по этой метке. Меня интересуют клики только внутри изображения и в позиции, выраженной числом от 0 до 1, 0 является крайним левым или верхним пикселем, а 1-крайним правым или нижним пикселем, независимо от фактического размера изображения.

Я не могу заставить прямоугольник изображения вычислить положение. Когда я звоню self.pixmap.rect() , ширина и высота-это исходные размеры изображения, а не размеры изображения, которое было масштабировано, чтобы вписаться в метку.

Что я делаю не так?

 from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QGridLayout
from PyQt5.QtGui import QPixmap

class Window(QWidget):
    def __init__(self):
        super().__init__()
        image_path = '092.jpg'
        image_area = Image_Area(QPixmap(image_path))
        image_area.left_click.connect(self.image_clicked)

        layout = QGridLayout()
        layout.addWidget(image_area, 0, 0)
        self.setLayout(layout)

    # Left click on image
    def image_clicked(self, x, y):
        print(f'{x:.3f},{y:.3f}')


class Image_Area(QLabel):

    left_click = pyqtSignal(float, float)

    def __init__(self, pixmap):
        super().__init__()
        self.pixmap = pixmap
        self.setPixmap(pixmap)
        self.setScaledContents(False)
        self.setMinimumSize(100, 100)

    def resizeEvent(self, e):
        if self.pixmap != None:
            width, height = int(self.width()), int(self.height())
            scaled_pixmap = self.pixmap.scaled(width, height, Qt.KeepAspectRatio)
            self.setPixmap(scaled_pixmap)

        return super().resizeEvent(e)

    def mousePressEvent(self, e):
        x, y, = e.x(), e.y()

        if self.pixmap:
            # Get pixmap rectangle
            r = self.pixmap.rect()
            x0, y0 = r.x(), r.y()
            x1, y1 = x0 r.width(), y0 r.height()

            # Check we clicked on the pixmap
            if x >= x0 and x < x1 and y >= y0 and y < y1:

                # emit position relative to pixmap bottom-left corner
                x_relative = (x - x0) / (x1 - x0)
                y_relative = (y - y0) / (y1 - y0)
                self.left_click.emit(x_relative, y_relative)

        super().mousePressEvent(e)


app = QApplication([])
window = Window()
window.show()
app.exec()
 

Обновление: После того, как был предоставлен ответ, я понял, что вычисление положения щелчка относительно источника изображения было неправильным, вот точная версия для заинтересованных лиц.:

 def mousePressEvent(self, e):
        # Mouse position is in label coordinates
        x, y, = e.x(), e.y()

        if self.pixmap():
            # pixmap is not a widget, we don't have its location,
            # so we assume the pixmap is centered in the label and
            # compute the location with respect to label
            label_size = self.size()
            pixmap_size = self.pixmap().size()
            width = pixmap_size.width()
            height = pixmap_size.height()

            x0 = int((label_size.width() - width) / 2)
            y0 = int((label_size.height() - height) / 2)

            # Check we clicked on the pixmap
            if (x >= x0 and x < (x0   width) and
                y >= y0 and y < (y0   height)):

                # emit position relative to pixmap top-left corner
                x_relative = (x - x0) / width
                y_relative = (y - y0) / height
                self.left_click.emit(x_relative, y_relative)

        super().mousePressEvent(e)
 

Ответ №1:

Вы должны использовать QPixmap, установленный в QLabel, который можно получить с помощью метода pixmap (). Проблема в том, что вы затрудняете доступ к этому методу, поскольку у вас есть атрибут с аналогичным именем. Поэтому решение состоит в том, чтобы использовать pixmap() и переименовать атрибут pixmap .

 class Image_Area(QLabel):

    left_click = pyqtSignal(float, float)

    def __init__(self, pixmap):
        super().__init__()
        self._pixmap = pixmap
        self.setPixmap(pixmap)
        self.setScaledContents(False)
        self.setMinimumSize(100, 100)

    def resizeEvent(self, e):
        if self._pixmap is not None:
            scaled_pixmap = self._pixmap.scaled(self.size(), Qt.KeepAspectRatio)
            self.setPixmap(scaled_pixmap)
        return super().resizeEvent(e)

    def mousePressEvent(self, e):
        x, y, = (
            e.x(),
            e.y(),
        )
        if self.pixmap:
            r = self.pixmap().rect()
            x0, y0 = r.x(), r.y()
            x1, y1 = x0   r.width(), y0   r.height()
            if x >= x0 and x < x1 and y >= y0 and y < y1:
                x_relative = (x - x0) / (x1 - x0)
                y_relative = (y - y0) / (y1 - y0)
                self.left_click.emit(x_relative, y_relative)
        super().mousePressEvent(e)