Exemplo n.º 1
0
def qimage_from_array(arr: np.ndarray) -> QImage:
    """
    Create and return an QImage from a (N, M, C) uint8 array where C is 3 for
    RGB and 4 for RGBA channels.

    Parameters
    ----------
    arr: (N, M C) uint8 array

    Returns
    -------
    image: QImage
        An QImage with size (M, N) in ARGB32 format format depending on `C`.
    """
    h, w, c = arr.shape
    if c == 4:
        format = QImage.Format_ARGB32
    elif c == 3:
        format = QImage.Format_RGB32
    else:
        raise ValueError(f"Wrong number of channels (need 3 or 4, got {c}")
    channels = arr.transpose((2, 0, 1))
    img = QImage(w, h, QImage.Format_ARGB32)
    img.fill(Qt.white)
    if img.size().isEmpty():
        return img
    buffer = img.bits().asarray(w * h * 4)
    view = np.frombuffer(buffer, np.uint32).reshape((h, w))
    if format == QImage.Format_ARGB32:
        view[:, :] = qrgba(*channels)
    elif format == QImage.Format_RGB32:
        view[:, :] = qrgb(*channels)
    return img
Exemplo n.º 2
0
def qimage_indexed_from_array(arr: np.ndarray,
                              colortable: Sequence[Sequence[int]]) -> QImage:
    arr = np.asarray(arr, dtype=np.uint8)
    h, w = arr.shape
    colortable = np.asarray(colortable, dtype=np.uint8)
    ncolors, nchannels = colortable.shape
    img = QImage(w, h, QImage.Format_Indexed8)
    img.setColorCount(ncolors)
    if nchannels == 4:
        qrgb_ = qrgba
    elif nchannels == 3:
        qrgb_ = qrgb
    else:
        raise ValueError

    for i, c in enumerate(colortable):
        img.setColor(i, qrgb_(*c))
    if img.size().isEmpty():
        return img
    buffer = img.bits().asarray(w * h)
    view = np.frombuffer(buffer, np.uint8).reshape((h, w))
    view[:, :] = arr
    return img