简体   繁体   English

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

[英]16-bit grayscale image handling with custom image provider in PySide2

I have a PySide2 project where I've defined a custom image provider, relaying images from a camera to an image control in QtQuick.我有一个 PySide2 项目,我在其中定义了一个自定义图像提供程序,将图像从相机中继到 QtQuick 中的图像控件。 The images are 16-bit grayscale arrays, so I'd prefer to simply relay this image data directly to the display.这些图像是 16 位灰度 arrays,所以我更愿意简单地将这些图像数据直接传递到显示器。 Raw image data (self.image) is processed and converted to a QImage via the following line:原始图像数据(self.image)通过以下行处理并转换为 QImage:

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

This, of course, requires that I resample the data from 16 bit (ushort) to 8 bit (uint) prior to display.当然,这要求我在显示之前将数据从 16 位 (ushort) 重新采样到 8 位 (uint)。 Instead, I tried calling directly the 16-bit format:相反,我尝试直接调用 16 位格式:

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

But this throws an error:但这会引发错误:

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

I'm running PySide2 version 5.13.2, and the Qt documentation says that Format_Grayscale16 was available from Qt 5.13 (not to mention it seems to exist in my local QImage class, since PyCharm gives me the auto-complete option). I'm running PySide2 version 5.13.2, and the Qt documentation says that Format_Grayscale16 was available from Qt 5.13 (not to mention it seems to exist in my local QImage class, since PyCharm gives me the auto-complete option). What am I doing wrong here?我在这里做错了什么?

It is recommended that when compiling a specific version of PySide2 then the same version of Qt is used, but that is not a mandatory rule so if the pyside2 that is used is not officially provided then the previous recommendation was not necessarily met.建议在编译特定版本的 PySide2 时,使用相同版本的 Qt,但这不是强制性规则,因此如果使用的 pyside2 没有官方提供,则不一定满足之前的建议。 If you want to know the version of pyside2 and qt you can use the following commands, respectively:如果想知道pyside2和qt的版本可以分别使用以下命令:

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

For PyQt5 you can use:对于 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)"

As the OP points out in the comments, he is using anaconda which compiles manually so it does not meet the initial recommendation (uses Qt 5.12.5).正如 OP 在评论中指出的那样,他使用的是手动编译的 anaconda,因此不符合最初的建议(使用 Qt 5.12.5)。 So the solution is to use official PySide2 using pip:所以解决方案是使用官方PySide2,使用pip:

python -m pip install pyside2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM