TheStrangeQuark
TheStrangeQuark

Reputation: 2405

What class to use for showing images in PyQt5

I've been trying to figure this out for awhile now, but cannot seem to figure it out. I am developing a program with a UI that I made in Qt Designer, where I will be analyzing images using cv2. Part of this analysis involves having the user click on the image to store different coordinates from the image to be used later. When I made the UI in Qt Designer, the image object is of the class QtWidgets.QGraphicsView, but I cannot figure out how to show an image from cv2, which is a numpy array on an object of type QtWidgets.QGraphicsView. I am thinking I should be using a QtGui.QPixmap, but I'm still not sure. It seems there is not great documentation, so I'm having troubles figuring out how to choose how to represent these images that I plan to have interactions with and analysis of.

Upvotes: 0

Views: 106

Answers (1)

TheStrangeQuark
TheStrangeQuark

Reputation: 2405

I did end up using QGraphicsPixmapItem as suggested by ekhumoro in the comments. I also used QImage to do this though. An example of how I'm doing this is

image_window = QtWidgets.QGraphicsView(centralwidget)
raw_image = cv2.imread('image.jpg')
height, width, channel = image.shape
bytes_per_line = width * 3
image = QtGui.QImage(raw_image.data, width, height, bytes_per_line, QtGui.QImage.Format_RGB888)
scene = QtWidgets.QGraphicsScene()
scene.addPixmap(QtGui.QPixmap.fromImage(image)
image_window.setScene(scene)

And this worked great for me. There may still be some optimizations I can do, but it gives me the freedom to keep everything separated so I can easily do manipulations to the raw_image pixel matrix. I also have everything abstracted by having a class for the scene and raw_image.

Upvotes: 1

Related Questions