当鼠标单击图像时,Pyqt获取像素位置和值 [英] Pyqt get pixel position and value when mouse click on the image

查看:1058
本文介绍了当鼠标单击图像时,Pyqt获取像素位置和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pixMapItem上覆盖鼠标事件对我不起作用; pixMapItem未检测到鼠标单击事件.这是我的代码:

Overwriting mouse event on pixMapItem didn't work for me ; the mouse click event is not detected by the pixMapItem. Here is my code :

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class DrawImage( QMainWindow ):
    def __init__(self, path):

        QMainWindow.__init__(self)
        self.setWindowTitle('Select Window')
        self.local_image = QImage(path)

        self.local_grview = QGraphicsView()
        self.setCentralWidget( self.local_grview )

        self.local_scene = QGraphicsScene()

        self.image_format = self.local_image.format()
        self.pixMapItem = self.local_scene.addPixmap( QPixmap(self.local_image) )
        self.local_grview.setScene( self.local_scene )




        self.pixMapItem.mousePressEvent = self.pixelSelect

        self.show()
        sys.exit(app.exec_())

    def pixelSelect( self, event ):
        print 'hello'
        position = QPoint( event.pos().x(),  event.pos().y())
        color = QColor.fromRgb(self.local_image.pixel( position ) )
        if color.isValid():
            rgbColor = '('+str(color.red())+','+str(color.green())+','+str(color.blue())+','+str(color.alpha())+')'
            self.setWindowTitle( 'Pixel position = (' + str( event.pos().x() ) + ' , ' + str( event.pos().y() )+ ') - Value (R,G,B,A)= ' + rgbColor)
        else:
            self.setWindowTitle( 'Pixel position = (' + str( event.pos().x() ) + ' , ' + str( event.pos().y() )+ ') - color not valid')

推荐答案

我已经尝试了您的代码,我相信如果进行更改,您就可以使它正常工作

I've tried your code, I believe you could get it working if change

self.pixMapItem = self.local_scene.addPixmap( QPixmap(self.local_image) )

行到

self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)

下面是您的代码的完整版本,对我而言效果很好:

below is a full version of your code which worked fine for me:

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

class DrawImage(QMainWindow): 
    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)

        self.setWindowTitle('Select Window')
        self.local_image = QImage('image_file_name.JPG')

        self.local_grview = QGraphicsView()
        self.setCentralWidget( self.local_grview )

        self.local_scene = QGraphicsScene()

        self.image_format = self.local_image.format()
        #self.pixMapItem = self.local_scene.addPixmap( QPixmap(self.local_image) )
        self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)

        self.local_grview.setScene( self.local_scene )

        self.pixMapItem.mousePressEvent = self.pixelSelect

    def pixelSelect( self, event ):
        print 'hello'
        position = QPoint( event.pos().x(),  event.pos().y())
        color = QColor.fromRgb(self.local_image.pixel( position ) )
        if color.isValid():
            rgbColor = '('+str(color.red())+','+str(color.green())+','+str(color.blue())+','+str(color.alpha())+')'
            self.setWindowTitle( 'Pixel position = (' + str( event.pos().x() ) + ' , ' + str( event.pos().y() )+ ') - Value (R,G,B,A)= ' + rgbColor)
        else:
            self.setWindowTitle( 'Pixel position = (' + str( event.pos().x() ) + ' , ' + str( event.pos().y() )+ ') - color not valid')


def main():
    app = QtGui.QApplication(sys.argv)
    form = DrawImage()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

希望这会有所帮助

这篇关于当鼠标单击图像时,Pyqt获取像素位置和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆