1

I'v been playing around with PyQt5 and got stuck here. In a certain menu, for each button click (and a given numpy image array), I'm trying to open a new image window (while converting the numpy array to an image).

So first, I did for one image (used this answer to convert numpy image array to QPixmap):

NewNumpyImageWindow.py class:

import cv2
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap, QImage


class NewImage(QWidget):

    def __init__(self, npImage):
        super().__init__()
        label = QLabel(self)
        pixmap = self.ConvertNumpyToQPixmap(npImage)
        label.setPixmap(pixmap)
        self.resize(pixmap.width(), pixmap.height())
        self.show()

    @staticmethod
    def ConvertNumpyToQPixmap(np_img):
        height, width, channel = np_img.shape
        bytesPerLine = 3 * width
        return QPixmap(QImage(np_img.data, width, height, bytesPerLine, QImage.Format_RGB888).rgbSwapped())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    currentNumpyImage = cv2.imread("capture.png")
    window = NewImage(currentNumpyImage)
    sys.exit(app.exec_())

And it works fine. The image shows up as a new window. Now, I want the image to show up as a new window each time I press a button in my main menu.

So I tried to create an instance of the class above for each press (on the button 'New'), and it doesn't work. It seems like the window shows up as a new window and closes immediately when the code finishes.

NewNumpyImageWindowMenu.py class:

import cv2
import sys
from PyQt5 import QtWidgets
from NewNumpyImageWindow import NewImage


class Menu(QtWidgets.QMainWindow):

    def __init__(self, numpyPic):
        super().__init__()
        newAct = QtWidgets.QAction('New', self)
        self.numpyPicture = numpyPic
        newAct.triggered.connect(self.newPicture)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(newAct)
        self.setGeometry(300, 300, 350, 250)
        self.show()

    def newPicture(self):
        NewImage(self.numpyPicture) #From the previous class


if __name__ == '__main__':
    currentNumpyImage = cv2.imread("capture.png")
    app = QtWidgets.QApplication(sys.argv)
    ex = Menu(currentNumpyImage)
    sys.exit(app.exec_())

Any help would be appreciated. Thanks.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
sheldonzy
  • 5,505
  • 9
  • 48
  • 86

1 Answers1

2

Try it:

NewNumpyImageWindowMenu.py

import cv2
import sys
from PyQt5 import QtWidgets
from NewNumpyImageWindow import NewImage


class Menu(QtWidgets.QMainWindow):

    def __init__(self, numpyPic):
        super().__init__()
        newAct = QtWidgets.QAction('New', self)
        self.numpyPicture = numpyPic
        newAct.triggered.connect(self.newPicture)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(newAct)
        self.setGeometry(300, 300, 350, 250)
        self.show()

    def newPicture(self):
        #NewImage(self.numpyPicture) #From the previous class  # ---
        self.newImage = NewImage(self.numpyPicture)            # +++


if __name__ == '__main__':
    #currentNumpyImage = cv2.imread("capture.png")
    currentNumpyImage = cv2.imread("logo.png")
    app = QtWidgets.QApplication(sys.argv)
    ex = Menu(currentNumpyImage)
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33