[英]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:简而言之:
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:总之:
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.