简体   繁体   English

bytearray 到 Python 中的 numpy 数组,用于在 pyqt5 GUI 中显示

[英]bytearray to numpy array in Python for displaying in pyqt5 GUI

I'm new to Python and have sorta c++ style coding background.我是 Python 的新手,并且有 c++ 风格的编码背景。

In short:简而言之:

  • tring to display 16 bit grayscale images in my GUI(pyqt6)尝试在我的 GUI(pyqt6)中显示 16 位灰度图像
  • have the stored the image data in bytearray, that is, 1024 by 768 (the total size of the byte array would be 1024 x 768 x 2 since it's 16 bit).将图像数据存储在 bytearray 中,即 1024 x 768(字节数组的总大小为 1024 x 768 x 2,因为它是 16 位)。
  • having difficulties with using this data = bytearray(1572864) for displaying.使用此数据 = bytearray(1572864) 进行显示时遇到困难。

The following code is what I have used for displaying the same image (but downscaled to 8-bit).以下代码是我用来显示相同图像的代码(但缩小到 8 位)。 What I'm now trying to do is use the 16-bit data and have it converted to QPixmap in order to display.我现在要做的是使用 16 位数据并将其转换为 QPixmap 以便显示。

The code for displaying 8-bit raw image is ( I have used rawpy for this, please feel free to get rid of the rawpy section if you could come up with a better way to do this)显示 8 位原始图像的代码是(我为此使用了 rawpy,如果您能想出更好的方法,请随时摆脱 rawpy 部分)

raw = np.array( binary data read from file)
src = raw.postprocess()
buf = src.data.tobytes()
h, w, ch = src.shape
bytesperline = ch * w
image = QImage(buf, 1024, 768, bytesperline, QImage.Format_RGB888)
self.DisplayRect.setPixmap(QPixmap.fromImage(image))
self.DisplayRect.show()

Hope I explained my question without causing any confusion.希望我在没有引起任何混乱的情况下解释了我的问题。 In summary:总之:

  • trying to use data in bytearray(1572864) to display in my GUI试图使用 bytearray(1572864) 中的数据在我的 GUI 中显示
  • in need of help with manipulating the binary data from/to different data structures需要帮助从/到不同的数据结构操作二进制数据

What I would have done if it were c++:如果是 c++,我会怎么做:

//declare a Mat with 16UC1 format
//easily memcpy(mat.data, source, size);
//stretchDiBits(dc, where_to_draw_imgs, mat.data);

Thank you very much in advance.非常感谢您提前。

Did you try using QImage.Format_Grayscale16 ?:您是否尝试使用QImage.Format_Grayscale16 ?:

from pathlib import Path
w, h = 1024, 768
raw_data = Path('image.raw').read_bytes()
data = bytearray(raw_data)
image = QImage(data, w, h, w, QImage.Format_Grayscale16)

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

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