Convert 16-bit grayscale to QImage

前端 未结 1 1657
余生分开走
余生分开走 2020-12-19 14:14

I am working on a sensor-based Python application built on a PyQt4 GUI. The sensor is generating 16-bit measurements... 256 16-bit \"pixels\" per \"line\". A square \"imag

相关标签:
1条回答
  • 2020-12-19 14:43

    Here I use some function data for demo:

    y, x = np.mgrid[-10:10:256j, -10:10:256j]
    data = ((np.sin(y**2 + x**2) + 2) * 1000).astype(np.uint16)
    
    img_8bit = (data / 256.0).astype(np.uint8) # use the high 8bit
    img_8bit = ((data - data.min()) / (data.ptp() / 255.0)).astype(np.uint8) # map the data range to 0 - 255
    img = QtGui.QImage(img_8bit.repeat(4), 256, 256, QtGui.QImage.Format_RGB32)
    

    When use the high 8bit, it looks like:

    enter image description here

    When map min & max value to (0, 255), it looks like:

    enter image description here

    To convert the 8bit image to 32bit, you can just call img_8bit.repeat(4), this will repeat every byte 4 times, so the memory can be viewed as an uint32 buffer. Since you create the QImage by Format_RGB32 not Format_ARGB32, the most significant byte is not used.

    0 讨论(0)
提交回复
热议问题