def _get_qimage(self, rgb_data): ht, wd, channels = rgb_data.shape result = QImage(rgb_data.data, wd, ht, self.qimg_fmt) # Need to hang on to a reference to the array result.ndarray = rgb_data return result
def rgb2qimage(rgb): """Convert the 3D numpy array `rgb` into a 32-bit QImage. `rgb` must have three dimensions with the vertical, horizontal and RGB image axes. ATTENTION: This QImage carries an attribute `ndarray` with a reference to the underlying numpy array that holds the data. On Windows, the conversion into a QPixmap does not copy the data, so that you have to take care that the QImage does not get garbage collected (otherwise PyQt will throw away the wrapper, effectively freeing the underlying memory - boom!).""" if len(rgb.shape) != 3: raise ValueError("rgb2QImage can only convert 3D arrays") if rgb.shape[2] not in (3, 4): raise ValueError("rgb2QImage can expects the last dimension to contain exactly three (R,G,B) or four (R,G,B,A) channels") h, w, channels = rgb.shape # Qt expects 32bit BGRA data for color images: bgra = numpy.empty((h, w, 4), numpy.uint8, 'C') bgra[...,0] = rgb[...,2] bgra[...,1] = rgb[...,1] bgra[...,2] = rgb[...,0] if rgb.shape[2] == 3: bgra[...,3].fill(255) fmt = QImage.Format_RGB32 else: bgra[...,3] = rgb[...,3] fmt = QImage.Format_ARGB32 result = QImage(bgra.data, w, h, fmt) result.ndarray = bgra return result
def _get_qimage(self, bgra): h, w, channels = bgra.shape fmt = QImage.Format_ARGB32 result = QImage(bgra.data, w, h, fmt) # Need to hang on to a reference to the array result.ndarray = bgra return result
def _get_qimage(self, rgb_data, format): rgb_data = np.ascontiguousarray(rgb_data) ht, wd, channels = rgb_data.shape result = QImage(rgb_data.data, wd, ht, format) # Need to hang on to a reference to the array result.ndarray = rgb_data return result
def gray2qimage(gray): """Convert the 2D numpy array `gray` into a 8-bit QImage with a gray colormap. The first dimension represents the vertical image axis. ATTENTION: This QImage carries an attribute `ndarray` with a reference to the underlying numpy array that holds the data. On Windows, the conversion into a QPixmap does not copy the data, so that you have to take care that the QImage does not get garbage collected (otherwise PyQt will throw away the wrapper, effectively freeing the underlying memory - boom!).""" if len(gray.shape) != 2: raise ValueError("gray2QImage can only convert 2D arrays") h, w = gray.shape bgra = numpy.empty((h, w, 4), numpy.uint8, 'C') bgra[...,0] = gray bgra[...,1] = gray bgra[...,2] = gray bgra[...,3].fill(255) fmt = QImage.Format_RGB32 result = QImage(bgra.data, w, h, fmt) result.ndarray = bgra return result