小编pol*_*ain的帖子

当鼠标点击图像时,Pyqt获取像素位置和值

我想知道如何在图像中单击鼠标(QImge)来选择像素,并获得像素位置和值.

谢谢

python image pixel pyqt selection

5
推荐指数
2
解决办法
1万
查看次数

当鼠标单击图像时,Pyqt 获取像素位置和值

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

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 = (' …
Run Code Online (Sandbox Code Playgroud)

mouse qt pixel click coordinate

2
推荐指数
1
解决办法
7200
查看次数

标签 统计

pixel ×2

click ×1

coordinate ×1

image ×1

mouse ×1

pyqt ×1

python ×1

qt ×1

selection ×1