如何结合这两个代码?(python图像查看器+鼠标拖动图像ROI裁剪)

我是 Python 和 PyQt 的初学者。我尝试基于 acbetter 的代码创建图像查看器,并希望添加图像裁剪功能。使用这两个代码,最终我想创建一个代码,我可以在其中打开某个图像并裁剪图像的特定部分并将其显示在另一个窗口中。你想帮助如何结合这两个代码?

  1. acbetter 的图像查看器代码 https://gist.github.com/acbetter/32c575803ec361c3e82064e60db4e3e0

使用opencv的图像裁剪代码

import cv2 

mouse_is_pressing = False

start_x, start_y = -1, -1


def mouse_callback(event, x, y, flags, param):

    global start_x, start_y, mouse_is_pressing 


    img_result = src.copy()


    if event == cv2.EVENT_LBUTTONDOWN:


        mouse_is_pressing = True

        start_x, start_y = x,y


        cv2.circle(img_result, (x,y), 10, (0,255,0),-1)

        cv2.imshow("img_color", img_result)


    elif event == cv2.EVENT_MOUSEMOVE:

        if mouse_is_pressing: 

            cv2.rectangle(img_result, (start_x, start_y), (x,y), (0,255,0), 3)

            cv2.imshow("img_color", img_result)


    elif event == cv2.EVENT_LBUTTONUP:

        mouse_is_pressing = False 


        img_part = img_result[start_y:y, start_x:x]

        cv2.imshow("img_color", img_result)

        cv2.imshow("img_part", img_part)


src = cv2.imread("D:/python data/image/image.jpg")

cv2.imshow("img_color", src)

cv2.setMouseCallback("img_color", mouse_callback)

cv2.waitKey(0)

cv2.destroyAllWindows()


aluckdog
浏览 68回答 1
1回答

largeQ

使用 QLabel 在基于 QScrollArea 的查看器中实现裁剪是不必要的复杂,因为转换很难跟踪,而是使用 QGraphicsView、QGraphicsScene 和 QGraphicsPixmapItem 实现相同的逻辑。如此多的逻辑已经实现,如下所示:from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupportclass Viewer(QtWidgets.QGraphicsView):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(QtWidgets.QGraphicsScene(), parent)&nbsp; &nbsp; &nbsp; &nbsp; self.pixmap_item = self.scene().addPixmap(QtGui.QPixmap())&nbsp; &nbsp; &nbsp; &nbsp; self.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)&nbsp; &nbsp; &nbsp; &nbsp; self.setBackgroundRole(QtGui.QPalette.Dark)&nbsp; &nbsp; &nbsp; &nbsp; self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)&nbsp; &nbsp; &nbsp; &nbsp; self.rubberBandChanged.connect(self.onRubberBandChanged)&nbsp; &nbsp; &nbsp; &nbsp; self.last_rect = QtCore.QPointF()&nbsp; &nbsp; def setPixmap(self, pixmap):&nbsp; &nbsp; &nbsp; &nbsp; self.pixmap_item.setPixmap(pixmap)&nbsp; &nbsp; def zoomIn(self):&nbsp; &nbsp; &nbsp; &nbsp; self.scale(1.25, 1.25)&nbsp; &nbsp; def zoomOut(self):&nbsp; &nbsp; &nbsp; &nbsp; self.scale(0.8, 0.8)&nbsp; &nbsp; def resetZoom(self):&nbsp; &nbsp; &nbsp; &nbsp; self.resetTransform()&nbsp; &nbsp; def fitToWindow(self):&nbsp; &nbsp; &nbsp; &nbsp; self.fitInView(self.pixmap_item)&nbsp; &nbsp; @QtCore.pyqtSlot(QtCore.QRect, QtCore.QPointF, QtCore.QPointF)&nbsp; &nbsp; def onRubberBandChanged(self, rubberBandRect, fromScenePoint, toScenePoint):&nbsp; &nbsp; &nbsp; &nbsp; if rubberBandRect.isNull():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixmap = self.pixmap_item.pixmap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rect = self.pixmap_item.mapFromScene(self.last_rect).boundingRect().toRect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not rect.intersected(pixmap.rect()).isNull():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; crop_pixmap = pixmap.copy(rect)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label = QtWidgets.QLabel(pixmap=crop_pixmap)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog = QtWidgets.QDialog(self)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lay = QtWidgets.QVBoxLayout(dialog)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lay.addWidget(label)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.exec_()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.last_rect = QtCore.QRectF()&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.last_rect = QtCore.QRectF(fromScenePoint, toScenePoint)class QImageViewer(QtWidgets.QMainWindow):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.view = Viewer()&nbsp; &nbsp; &nbsp; &nbsp; self.setCentralWidget(self.view)&nbsp; &nbsp; &nbsp; &nbsp; self.printer = QtPrintSupport.QPrinter()&nbsp; &nbsp; &nbsp; &nbsp; self.createActions()&nbsp; &nbsp; &nbsp; &nbsp; self.createMenus()&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("Image Viewer")&nbsp; &nbsp; &nbsp; &nbsp; self.resize(800, 600)&nbsp; &nbsp; def open(self):&nbsp; &nbsp; &nbsp; &nbsp; fileName, _ = QtWidgets.QFileDialog.getOpenFileName(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "QFileDialog.getOpenFileName()",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Images (*.png *.jpeg *.jpg *.bmp *.gif)",&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; if fileName:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixmap = QtGui.QPixmap(fileName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pixmap.isNull():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QtWidgets.QMessageBox.information(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self, "Image Viewer", "Cannot load %s." % fileName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.view.setPixmap(pixmap)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.printAct.setEnabled(True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.fitToWindowAct.setEnabled(True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.updateActions()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not self.fitToWindowAct.isChecked():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # self.imageLabel.adjustSize()&nbsp; &nbsp; def print_(self):&nbsp; &nbsp; &nbsp; &nbsp; dialog = QtPrintSupport.QPrintDialog(self.printer, self)&nbsp; &nbsp; &nbsp; &nbsp; if dialog.exec_():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixmap = self.view.pixmap_item.pixmap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter = QtGui.QPainter(self.printer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rect = painter.viewport()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = pixmap.size()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size.scale(rect.size(), QtCore.Qt.KeepAspectRatio)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter.setViewport(rect.x(), rect.y(), size.width(), size.height())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter.setWindow(pixmap.rect())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter.drawPixmap(0, 0, pixmap)&nbsp; &nbsp; def fitToWindow(self):&nbsp; &nbsp; &nbsp; &nbsp; if self.fitToWindowAct.isChecked():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.view.fitToWindow()&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.view.resetZoom()&nbsp; &nbsp; &nbsp; &nbsp; self.updateActions()&nbsp; &nbsp; def about(self):&nbsp; &nbsp; &nbsp; &nbsp; QtWidgets.QMessageBox.about(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "About Image Viewer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<p>The <b>Image Viewer</b> example shows how to combine "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "QLabel and QScrollArea to display an image. QLabel is "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "typically used for displaying text, but it can also display "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "an image. QScrollArea provides a scrolling view around "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "another widget. If the child widget exceeds the size of the "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "frame, QScrollArea automatically provides scroll bars.</p>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<p>The example demonstrates how QLabel's ability to scale "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "its contents (QLabel.scaledContents), and QScrollArea's "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ability to automatically resize its contents "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "(QScrollArea.widgetResizable), can be used to implement "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "zooming and scaling features.</p>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<p>In addition the example shows how to use QPainter to "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "print an image.</p>",&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; def createActions(self):&nbsp; &nbsp; &nbsp; &nbsp; self.openAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&Open...", self, shortcut="Ctrl+O", triggered=self.open&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.printAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&Print...", self, shortcut="Ctrl+P", enabled=False, triggered=self.print_&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.exitAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "E&xit", self, shortcut="Ctrl+Q", triggered=self.close&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.zoomInAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Zoom &In (25%)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortcut="Ctrl++",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggered=self.view.zoomIn,&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.zoomOutAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Zoom &Out (25%)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortcut="Ctrl+-",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggered=self.view.zoomOut,&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.normalSizeAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&Normal Size",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortcut="Ctrl+S",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggered=self.view.resetZoom,&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.fitToWindowAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&Fit to Window",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkable=True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortcut="Ctrl+F",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triggered=self.fitToWindow,&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.aboutAct = QtWidgets.QAction("&About", self, triggered=self.about)&nbsp; &nbsp; &nbsp; &nbsp; self.aboutQtAct = QtWidgets.QAction(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "About &Qt", self, triggered=QtWidgets.qApp.aboutQt&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; def createMenus(self):&nbsp; &nbsp; &nbsp; &nbsp; self.fileMenu = QtWidgets.QMenu("&File", self)&nbsp; &nbsp; &nbsp; &nbsp; self.fileMenu.addAction(self.openAct)&nbsp; &nbsp; &nbsp; &nbsp; self.fileMenu.addAction(self.printAct)&nbsp; &nbsp; &nbsp; &nbsp; self.fileMenu.addSeparator()&nbsp; &nbsp; &nbsp; &nbsp; self.fileMenu.addAction(self.exitAct)&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu = QtWidgets.QMenu("&View", self)&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu.addAction(self.zoomInAct)&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu.addAction(self.zoomOutAct)&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu.addAction(self.normalSizeAct)&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu.addSeparator()&nbsp; &nbsp; &nbsp; &nbsp; self.viewMenu.addAction(self.fitToWindowAct)&nbsp; &nbsp; &nbsp; &nbsp; self.helpMenu = QtWidgets.QMenu("&Help", self)&nbsp; &nbsp; &nbsp; &nbsp; self.helpMenu.addAction(self.aboutAct)&nbsp; &nbsp; &nbsp; &nbsp; self.helpMenu.addAction(self.aboutQtAct)&nbsp; &nbsp; &nbsp; &nbsp; self.menuBar().addMenu(self.fileMenu)&nbsp; &nbsp; &nbsp; &nbsp; self.menuBar().addMenu(self.viewMenu)&nbsp; &nbsp; &nbsp; &nbsp; self.menuBar().addMenu(self.helpMenu)&nbsp; &nbsp; def updateActions(self):&nbsp; &nbsp; &nbsp; &nbsp; self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())&nbsp; &nbsp; &nbsp; &nbsp; self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())&nbsp; &nbsp; &nbsp; &nbsp; self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())if __name__ == "__main__":&nbsp; &nbsp; import sys&nbsp; &nbsp; from PyQt5.QtWidgets import QApplication&nbsp; &nbsp; app = QApplication(sys.argv)&nbsp; &nbsp; imageViewer = QImageViewer()&nbsp; &nbsp; imageViewer.show()&nbsp; &nbsp; sys.exit(app.exec_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python