关于旋转图像的pyqt4像素信息

时间:2016-12-19 15:41:04

标签: image pyqt4

我已经调整了图像查看器(请参阅下面的代码),以便从加载的图像中获取像素信息。使用“加载图像”按钮加载图像,然后可以使用滚轮进行放大和缩小,使用鼠标左键单击并拖动进行平移。当您按下“输入像素信息模式”按钮时,拖动被禁用(您仍然可以缩放),单击图像将给出像素坐标(整数像素索引)和像素的灰度值。

问题是,如果旋转图像,按“旋转图像”按钮,使用像素信息按钮不再提供正确的像素信息。我想mapToScene方法不适合在旋转的图像上使用,但却无法找到其他方法。我尝试过各种各样的东西,比如在旋转的像素图上使用toImage()然后用这个替换原始图像,但似乎没有任何效果。解决这个问题的最佳方法是什么?

代码:

from PyQt4 import QtCore, QtGui

class PhotoViewer(QtGui.QGraphicsView):
    photoClicked = QtCore.pyqtSignal(QtCore.QPoint)

    def __init__(self, parent):
        super(PhotoViewer, self).__init__(parent)
        self._zoom = 0
        self._empty = True
        self._scene = QtGui.QGraphicsScene(self)
        self._photo = QtGui.QGraphicsPixmapItem()
        self._scene.addItem(self._photo)
        self.setScene(self._scene)
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
        self.setFrameShape(QtGui.QFrame.NoFrame)

    def fitInView(self):
        rect = QtCore.QRectF(self._photo.pixmap().rect())
        if not rect.isNull():
            unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
            self.scale(1 / unity.width(), 1 / unity.height())
            viewrect = self.viewport().rect()
            scenerect = self.transform().mapRect(rect)
            factor = min(viewrect.width() / scenerect.width(),
                         viewrect.height() / scenerect.height())
            self.scale(factor, factor)
            self.centerOn(rect.center())
            self._zoom = 0

    def hasPhoto(self):
        return not self._empty

    def toggleDragMode(self):
        if self.dragMode() == QtGui.QGraphicsView.ScrollHandDrag:
            self.setDragMode(QtGui.QGraphicsView.NoDrag)
        elif self.hasPhoto():
            self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)

    def setPhoto(self, pixmap=None):
        self._zoom = 0
        if pixmap and not pixmap.isNull():
            self._empty = False
            self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
            self._photo.setPixmap(pixmap)
            self.fitInView()
        else:
            self._empty = True
            self.setDragMode(QtGui.QGraphicsView.NoDrag)
            self._photo.setPixmap(QtGui.QPixmap())

    def wheelEvent(self, event):
        if not self._photo.pixmap().isNull():
            if event.delta() > 0:
                factor = 1.25
                self._zoom += 1
            else:
                factor = 0.8
                self._zoom -= 1
            if self._zoom > 0:
                self.scale(factor, factor)
            elif self._zoom == 0:
                self.fitInView()
            else:
                self._zoom = 0

    def mousePressEvent(self, event):
        if (self.hasPhoto() and
            self.dragMode() == QtGui.QGraphicsView.NoDrag and
            self._photo.isUnderMouse()):
            self.photoClicked.emit(QtCore.QPoint(event.pos()))
        super(PhotoViewer, self).mousePressEvent(event)

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.viewer = PhotoViewer(self)
        # 'Load image' button
        self.btnLoad = QtGui.QToolButton(self)
        self.btnLoad.setText('Load image')
        self.btnLoad.clicked.connect(self.loadImage)
        # Button to change from drag/pan to getting pixel info
        self.btnPixInfo = QtGui.QToolButton(self)
        self.btnPixInfo.setText('Enter pixel info mode')
        self.btnPixInfo.clicked.connect(self.pixInfo)        
        self.editPixInfo = QtGui.QLineEdit(self)
        self.editPixInfo.setReadOnly(True)
        # Button to rotate image by 10 degrees
        self.btnRotate = QtGui.QToolButton(self)
        self.btnRotate.setText('Rotate image')
        self.btnRotate.clicked.connect(self.rotateImage)
        self.viewer.photoClicked.connect(self.photoClicked)
        # Arrange layout
        VBlayout = QtGui.QVBoxLayout(self)
        VBlayout.addWidget(self.viewer)
        HBlayout = QtGui.QHBoxLayout()
        HBlayout.setAlignment(QtCore.Qt.AlignLeft)
        HBlayout.addWidget(self.btnLoad)
        HBlayout.addWidget(self.btnRotate)
        HBlayout.addWidget(self.btnPixInfo)
        HBlayout.addWidget(self.editPixInfo)
        VBlayout.addLayout(HBlayout)

    def loadImage(self):
        self.viewer.setPhoto(QtGui.QPixmap('pic.jpg'))

    def pixInfo(self):
        self.viewer.toggleDragMode()

    def rotateImage(self):
        self.viewer._photo.setRotation(10)

    def photoClicked(self, pos):
        pos = self.viewer.mapToScene(pos)
        # p.s. I realise the following lines are probably a very convoluted way of getting
        # a grayscale value from RGB, but I couldn't make it work any other way I tried
        rot_image = self.viewer._photo.pixmap().toImage().pixel(pos.x(), pos.y())
        colour = QtGui.QColor.fromRgb(rot_image)
        gsval = QtGui.qGray(colour.red(), colour.green(), colour.blue())
        self.editPixInfo.setText('X:%d, Y:%d Grayscale: %d' % (pos.x(), pos.y(), gsval))

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 600)
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您需要将场景坐标映射到项目坐标:

pos = self.viewer._photo.mapFromScene(self.viewer.mapToScene(pos))