久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<small id='55OAY'></small><noframes id='55OAY'>

    <tfoot id='55OAY'></tfoot>

      1. <i id='55OAY'><tr id='55OAY'><dt id='55OAY'><q id='55OAY'><span id='55OAY'><b id='55OAY'><form id='55OAY'><ins id='55OAY'></ins><ul id='55OAY'></ul><sub id='55OAY'></sub></form><legend id='55OAY'></legend><bdo id='55OAY'><pre id='55OAY'><center id='55OAY'></center></pre></bdo></b><th id='55OAY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='55OAY'><tfoot id='55OAY'></tfoot><dl id='55OAY'><fieldset id='55OAY'></fieldset></dl></div>
        <legend id='55OAY'><style id='55OAY'><dir id='55OAY'><q id='55OAY'></q></dir></style></legend>

        • <bdo id='55OAY'></bdo><ul id='55OAY'></ul>
      2. 如何結(jié)合這兩個(gè)代碼?(python圖像查看器+鼠標(biāo)拖動(dòng)

        How can I combine these two codes? (python image viewer + image ROI crop by mouse drag)(如何結(jié)合這兩個(gè)代碼?(python圖像查看器+鼠標(biāo)拖動(dòng)圖像ROI裁剪))
        <legend id='msOlc'><style id='msOlc'><dir id='msOlc'><q id='msOlc'></q></dir></style></legend>
      3. <tfoot id='msOlc'></tfoot>
              <tbody id='msOlc'></tbody>

              • <bdo id='msOlc'></bdo><ul id='msOlc'></ul>

                <i id='msOlc'><tr id='msOlc'><dt id='msOlc'><q id='msOlc'><span id='msOlc'><b id='msOlc'><form id='msOlc'><ins id='msOlc'></ins><ul id='msOlc'></ul><sub id='msOlc'></sub></form><legend id='msOlc'></legend><bdo id='msOlc'><pre id='msOlc'><center id='msOlc'></center></pre></bdo></b><th id='msOlc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='msOlc'><tfoot id='msOlc'></tfoot><dl id='msOlc'><fieldset id='msOlc'></fieldset></dl></div>
              • <small id='msOlc'></small><noframes id='msOlc'>

                  本文介紹了如何結(jié)合這兩個(gè)代碼?(python圖像查看器+鼠標(biāo)拖動(dòng)圖像ROI裁剪)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我是 Python 和 PyQt 的初學(xué)者.我嘗試基于 acbetter 的代碼創(chuàng)建圖像查看器,并希望添加圖像裁剪功能.使用這兩個(gè)代碼,最終我想創(chuàng)建一個(gè)代碼,我可以在其中打開某個(gè)圖像并裁剪圖像的特定部分并將其顯示在另一個(gè)窗口中.您想幫忙如何組合這兩個(gè)代碼嗎?

                  1. acbetter 的圖片查看器代碼

                    I'm beginner in Python and PyQt. I try to create image viewer based on acbetter's code and want to add image crop function. Using these two codes, ultimately I want to create a code in which I can open a certain image and crop specific part of the image and show it in another window. Would you like to help how to combine these two codes?

                    1. acbetter's image viewer code https://gist.github.com/acbetter/32c575803ec361c3e82064e60db4e3e0

                    from PyQt5.QtCore import Qt
                    from PyQt5.QtGui import QImage, QPixmap, QPalette, QPainter
                    from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
                    from PyQt5.QtWidgets import QLabel, QSizePolicy, QScrollArea, QMessageBox, QMainWindow, QMenu, QAction, 
                        qApp, QFileDialog
                    
                    
                    class QImageViewer(QMainWindow):
                        def __init__(self):
                            super().__init__()
                    
                            self.printer = QPrinter()
                            self.scaleFactor = 0.0
                    
                            self.imageLabel = QLabel()
                            self.imageLabel.setBackgroundRole(QPalette.Base)
                            self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
                            self.imageLabel.setScaledContents(True)
                    
                            self.scrollArea = QScrollArea()
                            self.scrollArea.setBackgroundRole(QPalette.Dark)
                            self.scrollArea.setWidget(self.imageLabel)
                            self.scrollArea.setVisible(False)
                    
                            self.setCentralWidget(self.scrollArea)
                    
                            self.createActions()
                            self.createMenus()
                    
                            self.setWindowTitle("Image Viewer")
                            self.resize(800, 600)
                    
                        def open(self):
                            options = QFileDialog.Options()
                            # fileName = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
                            fileName, _ = QFileDialog.getOpenFileName(self, 'QFileDialog.getOpenFileName()', '',
                                                                      'Images (*.png *.jpeg *.jpg *.bmp *.gif)', options=options)
                            if fileName:
                                image = QImage(fileName)
                                if image.isNull():
                                    QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
                                    return
                    
                                self.imageLabel.setPixmap(QPixmap.fromImage(image))
                                self.scaleFactor = 1.0
                    
                                self.scrollArea.setVisible(True)
                                self.printAct.setEnabled(True)
                                self.fitToWindowAct.setEnabled(True)
                                self.updateActions()
                    
                                if not self.fitToWindowAct.isChecked():
                                    self.imageLabel.adjustSize()
                    
                        def print_(self):
                            dialog = QPrintDialog(self.printer, self)
                            if dialog.exec_():
                                painter = QPainter(self.printer)
                                rect = painter.viewport()
                                size = self.imageLabel.pixmap().size()
                                size.scale(rect.size(), Qt.KeepAspectRatio)
                                painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
                                painter.setWindow(self.imageLabel.pixmap().rect())
                                painter.drawPixmap(0, 0, self.imageLabel.pixmap())
                    
                        def zoomIn(self):
                            self.scaleImage(1.25)
                    
                        def zoomOut(self):
                            self.scaleImage(0.8)
                    
                        def normalSize(self):
                            self.imageLabel.adjustSize()
                            self.scaleFactor = 1.0
                    
                        def fitToWindow(self):
                            fitToWindow = self.fitToWindowAct.isChecked()
                            self.scrollArea.setWidgetResizable(fitToWindow)
                            if not fitToWindow:
                                self.normalSize()
                    
                            self.updateActions()
                    
                        def about(self):
                            QMessageBox.about(self, "About Image Viewer",
                                              "<p>The <b>Image Viewer</b> example shows how to combine "
                                              "QLabel and QScrollArea to display an image. QLabel is "
                                              "typically used for displaying text, but it can also display "
                                              "an image. QScrollArea provides a scrolling view around "
                                              "another widget. If the child widget exceeds the size of the "
                                              "frame, QScrollArea automatically provides scroll bars.</p>"
                                              "<p>The example demonstrates how QLabel's ability to scale "
                                              "its contents (QLabel.scaledContents), and QScrollArea's "
                                              "ability to automatically resize its contents "
                                              "(QScrollArea.widgetResizable), can be used to implement "
                                              "zooming and scaling features.</p>"
                                              "<p>In addition the example shows how to use QPainter to "
                                              "print an image.</p>")
                    
                        def createActions(self):
                            self.openAct = QAction("&Open...", self, shortcut="Ctrl+O", triggered=self.open)
                            self.printAct = QAction("&Print...", self, shortcut="Ctrl+P", enabled=False, triggered=self.print_)
                            self.exitAct = QAction("E&xit", self, shortcut="Ctrl+Q", triggered=self.close)
                            self.zoomInAct = QAction("Zoom &In (25%)", self, shortcut="Ctrl++", enabled=False, triggered=self.zoomIn)
                            self.zoomOutAct = QAction("Zoom &Out (25%)", self, shortcut="Ctrl+-", enabled=False, triggered=self.zoomOut)
                            self.normalSizeAct = QAction("&Normal Size", self, shortcut="Ctrl+S", enabled=False, triggered=self.normalSize)
                            self.fitToWindowAct = QAction("&Fit to Window", self, enabled=False, checkable=True, shortcut="Ctrl+F",
                                                          triggered=self.fitToWindow)
                            self.aboutAct = QAction("&About", self, triggered=self.about)
                            self.aboutQtAct = QAction("About &Qt", self, triggered=qApp.aboutQt)
                    
                        def createMenus(self):
                            self.fileMenu = QMenu("&File", self)
                            self.fileMenu.addAction(self.openAct)
                            self.fileMenu.addAction(self.printAct)
                            self.fileMenu.addSeparator()
                            self.fileMenu.addAction(self.exitAct)
                    
                            self.viewMenu = QMenu("&View", self)
                            self.viewMenu.addAction(self.zoomInAct)
                            self.viewMenu.addAction(self.zoomOutAct)
                            self.viewMenu.addAction(self.normalSizeAct)
                            self.viewMenu.addSeparator()
                            self.viewMenu.addAction(self.fitToWindowAct)
                    
                            self.helpMenu = QMenu("&Help", self)
                            self.helpMenu.addAction(self.aboutAct)
                            self.helpMenu.addAction(self.aboutQtAct)
                    
                            self.menuBar().addMenu(self.fileMenu)
                            self.menuBar().addMenu(self.viewMenu)
                            self.menuBar().addMenu(self.helpMenu)
                    
                        def updateActions(self):
                            self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())
                            self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())
                            self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())
                    
                        def scaleImage(self, factor):
                            self.scaleFactor *= factor
                            self.imageLabel.resize(self.scaleFactor * self.imageLabel.pixmap().size())
                    
                            self.adjustScrollBar(self.scrollArea.horizontalScrollBar(), factor)
                            self.adjustScrollBar(self.scrollArea.verticalScrollBar(), factor)
                    
                            self.zoomInAct.setEnabled(self.scaleFactor < 3.0)
                            self.zoomOutAct.setEnabled(self.scaleFactor > 0.333)
                    
                        def adjustScrollBar(self, scrollBar, factor):
                            scrollBar.setValue(int(factor * scrollBar.value()
                                                   + ((factor - 1) * scrollBar.pageStep() / 2)))
                    
                    
                    if __name__ == '__main__':
                        import sys
                        from PyQt5.QtWidgets import QApplication
                    
                        app = QApplication(sys.argv)
                        imageViewer = QImageViewer()
                        imageViewer.show()
                        sys.exit(app.exec_())
                    

                    1. image crop code using 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()
                    

                    解決方案

                    Implementing the crop in a viewer based on a QScrollArea with a QLabel is unnecessarily complicated since the transformation is complicated to track, instead implement the same logic with QGraphicsView, QGraphicsScene and QGraphicsPixmapItem. So much of the logic is already implemented as I show below:

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

                    這篇關(guān)于如何結(jié)合這兩個(gè)代碼?(python圖像查看器+鼠標(biāo)拖動(dòng)圖像ROI裁剪)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                    【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動(dòng)后進(jìn)度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標(biāo)簽設(shè)置在固定位置,以便當(dāng)我向左或向右滾動(dòng)時(shí),yaxis 刻度標(biāo)簽應(yīng)該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時(shí)顯示進(jìn)度條?)
                  • <tfoot id='7Cw4x'></tfoot>

                        <tbody id='7Cw4x'></tbody>

                        <small id='7Cw4x'></small><noframes id='7Cw4x'>

                          <legend id='7Cw4x'><style id='7Cw4x'><dir id='7Cw4x'><q id='7Cw4x'></q></dir></style></legend>
                            <bdo id='7Cw4x'></bdo><ul id='7Cw4x'></ul>
                          • <i id='7Cw4x'><tr id='7Cw4x'><dt id='7Cw4x'><q id='7Cw4x'><span id='7Cw4x'><b id='7Cw4x'><form id='7Cw4x'><ins id='7Cw4x'></ins><ul id='7Cw4x'></ul><sub id='7Cw4x'></sub></form><legend id='7Cw4x'></legend><bdo id='7Cw4x'><pre id='7Cw4x'><center id='7Cw4x'></center></pre></bdo></b><th id='7Cw4x'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7Cw4x'><tfoot id='7Cw4x'></tfoot><dl id='7Cw4x'><fieldset id='7Cw4x'></fieldset></dl></div>

                            主站蜘蛛池模板: 欧美日韩精品久久久免费观看 | 欧美一区二区精品 | 在线观看一区 | 992tv在线| 日韩福利视频 | 日韩高清一区 | 欧美一区二区三 | 免费看黄色一级片 | 午夜视频福利 | 一级免费看 | 激情久久五月天 | 韩国三级中文字幕hd久久精品 | 一本一道久久a久久精品蜜桃 | 欧美日韩小视频 | 伊人精品久久 | 国产成人片 | 免费性视频 | 欧美日韩成人在线观看 | 国产成人av在线 | 日韩精品久久久久 | 亚洲第十页 | 久草福利在线观看 | 亚洲香蕉在线 | 一级片在线播放 | 日本一级大毛片a一 | 天天爱天天操 | 欧美成人一区二区三区 | 三级黄色片网站 | 精品国产区一区二 | 国产精品国产三级国产 | 三上悠亚激情av一区二区三区 | 欧美久久一区二区 | 激情综合婷婷 | 欧美黄视频 | 欧美成人精品欧美一级乱黄 | 亚洲伊人影院 | 一级片黄色片 | 亚洲福利片 | 色综合久久88色综合天天 | 大桥未久在线视频 | 国产视频在线播放 |