, , , , , :
self.img = QImage('fname.png')
pixmap = QPixmap(QPixmap.fromImage(self.img))
img_label = QLabel()
img_label.setPixmap(pixmap)
img_label.mousePressEvent = self.getPixel
def self.getPixel(self, event):
x = event.pos().x()
y = event.pos().y()
c = self.img.pixel(x,y)
c_qobj = QColor(c)
c_rgb = QColor(c).getRgb()
c_rgbf = QColor(c).getRgbf()
return x, y, c_rgb
Make sure the size of the label matches the size of the image, otherwise the x and y coordinates of the mouse must be converted to the coordinates of the image. And I think it is also possible to use the method .pixel()directly on pixmap, but the QImage object seems to work better in my case.
source
share