在 PySide2 中使用自定义图像提供程序处理 16 位灰度图像

16-bit grayscale image handling with custom image provider in PySide2

我有一个 PySide2 项目,我在其中定义了一个自定义图像提供程序,将图像从相机中继到 QtQuick 中的图像控件。这些图像是 16 位灰度阵列,因此我更愿意将此图像数据直接中继到显示器。原始图像数据 (self.image) 通过以下行处理并转换为 QImage:

img = QImage(self.image, w, h, line_len, QImage.Format_Grayscale8)

当然,这需要我在显示之前将数据从 16 位 (ushort) 重新采样到 8 位 (uint)。相反,我尝试直接调用 16 位格式:

img = QImage(self.image, w, h, line_len, QImage.Format_Grayscale16)

但这会引发错误:

AttributeError: type object 'PySide2.QtGui.QImage' has no attribute 'Format_Grayscale16'

我是 运行 PySide2 版本 5.13.2,Qt 文档说 Format_Grayscale16 可从 Qt 5.13 获得(更不用说它似乎存在于我的本地 QImage class,因为 PyCharm 给了我自动完成选项)。我在这里做错了什么?

建议在编译特定版本的PySide2时使用相同版本的Qt,但这不是强制性的规则,所以如果使用的pyside2不是官方提供的那么之前的建议不一定遇见了。如果想知道pyside2和qt的版本可以分别使用以下命令:

python -c "from PySide2 import __version__; print('PySide2 version', __version__)"
python -c "from PySide2.QtCore import qVersion; print('Qt version', qVersion())"

对于 PyQt5,您可以使用:

python -c "from PyQt5.QtCore import PYQT_VERSION_STR; print('PyQt5 version', PYQT_VERSION_STR)"
python -c "from PyQt5.QtCore import QT_VERSION_STR; print('Qt version', QT_VERSION_STR)"

正如 OP 在评论中指出的那样,他使用的是手动编译的 anaconda,因此不符合最初的建议(使用 Qt 5.12.5)。所以解决方案是使用pip使用官方PySide2:

python -m pip install pyside2