如何在PyQt4中滚动显示RGBA numpy数组图像?

时间:2021-08-21 21:20:15

I would like to display, in my custom window, two images in PyQt4. In the left side, a source image. In the right side, a transformed -transformation will be pixel-wise- image. The images will be in RGBA format loaded by PIL(low), and for numpy (scipy.ndimage.imread).

我想在我的自定义窗口中显示PyQt4中的两个图像。在左侧,是源图像。在右侧,转换后的转换将是像素方式的图像。图像将采用由PIL(低)加载的RGBA格式,以及numpy(scipy.ndimage.imread)。

Both images have the same size (since the transformed image will be generated from the source image), and both displays will have the same size (independent of the image size):

两个图像具有相同的大小(因为变换的图像将从源图像生成),并且两个显示将具有相同的大小(独立于图像大小):

  • If the image width is lower than display's, the image will be left-centered in the display. If it is greater, I need a scroll for the source display.
  • 如果图像宽度低于显示屏,则图像将在显示屏中保持居中。如果它更大,我需要一个滚动源显示。

  • An analogous criterion for height, top-centered, and vertical scroll bars.
  • 高度,顶部居中和垂直滚动条的类似标准。

  • If I scroll in the source display, the transformed's display will scroll as well, but the transformed's display will not have scrollbars.
  • 如果我在源显示中滚动,转换后的显示也会滚动,但转换后的显示将没有滚动条。

Question: What component could I use to display an image with inner scrolling capabilities? (I will be happy with the component name, and some guidelines; code would be appreciated but the main concept is the important thing for the answer).

问题:我可以使用哪个组件来显示具有内部滚动功能的图像? (我会对组件名称和一些指南感到满意;代码将受到赞赏,但主要概念是答案的重要事项)。

1 个解决方案

#1


2  

There are several solutions. If you just want to create the numpy "image" once and then display it, and it's not too big (or you don't care about memory), you can simply convert it to a QPixmap and display that in a viewport: Convert numpy array to PySide QPixmap

有几种解决方案。如果你只想创建一个numpy“image”然后显示它,并且它不是太大(或者你不关心内存),你可以简单地将它转换为QPixmap并在视口中显示:转换numpy数组到PySide QPixmap

    self.imageLabel = QtGui.QLabel()
    self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
    self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored,
            QtGui.QSizePolicy.Ignored) # not sure about this one.
    self.imageLabel.setPixmap(...pixmap here...)

    self.scrollArea = QtGui.QScrollArea()
    self.scrollArea.setBackgroundRole(QtGui.QPalette.Dark)
    self.scrollArea.setWidget(self.imageLabel)

(taken from http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/examples/widgets/imageviewer.py)

(取自http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/examples/widgets/imageviewer.py)

If the image is too big, then you can add a paint hook. In the hook, get the visible part of the QScrollArea (the viewport), turn just this into a pixmap and render it.

如果图像太大,则可以添加绘制钩子。在钩子中,获取QScrollArea(视口)的可见部分,将其转换为像素图并渲染它。

The same is true when the numpy array changes all the time. You need to trigger a refresh when the array has been updated. Qt will then call the paint hook and the new content will become visible.

当numpy数组一直在变化时也是如此。您需要在更新阵列时触发刷新。然后Qt将调用paint hook,新内容将变为可见。

#1


2  

There are several solutions. If you just want to create the numpy "image" once and then display it, and it's not too big (or you don't care about memory), you can simply convert it to a QPixmap and display that in a viewport: Convert numpy array to PySide QPixmap

有几种解决方案。如果你只想创建一个numpy“image”然后显示它,并且它不是太大(或者你不关心内存),你可以简单地将它转换为QPixmap并在视口中显示:转换numpy数组到PySide QPixmap

    self.imageLabel = QtGui.QLabel()
    self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
    self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored,
            QtGui.QSizePolicy.Ignored) # not sure about this one.
    self.imageLabel.setPixmap(...pixmap here...)

    self.scrollArea = QtGui.QScrollArea()
    self.scrollArea.setBackgroundRole(QtGui.QPalette.Dark)
    self.scrollArea.setWidget(self.imageLabel)

(taken from http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/examples/widgets/imageviewer.py)

(取自http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/examples/widgets/imageviewer.py)

If the image is too big, then you can add a paint hook. In the hook, get the visible part of the QScrollArea (the viewport), turn just this into a pixmap and render it.

如果图像太大,则可以添加绘制钩子。在钩子中,获取QScrollArea(视口)的可见部分,将其转换为像素图并渲染它。

The same is true when the numpy array changes all the time. You need to trigger a refresh when the array has been updated. Qt will then call the paint hook and the new content will become visible.

当numpy数组一直在变化时也是如此。您需要在更新阵列时触发刷新。然后Qt将调用paint hook,新内容将变为可见。