def toQImage(data, copy=True):
    if (data is None):
        return QImage()
    data = data.copy()
    data[(data > 255)] = 255
    data[(data < 0)] = 0
    data = data.astype(np.uint8)
    if (data.dtype == np.uint8):
        if (len(data.shape) == 2):
            qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_Indexed8)
            qim.setColorTable(gray_color_table)
            return (qim.copy() if copy else qim)
        elif (len(data.shape) == 3):
            if (data.shape[2] == 1):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_Grayscale8)
                return (qim.copy() if copy else qim)
            if (data.shape[2] == 3):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_RGB888)
                return (qim.copy() if copy else qim)
            elif (data.shape[2] == 4):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_ARGB32)
                return (qim.copy() if copy else qim)
            else:
                raise Exception(('Conversion of %d channel array to QImage not implemented' % data.shape[2]))
    raise Exception(('Conversion of %d dimension array to QImage not implemented' % len(data.shape)))
def toQImage(data, copy=True):
    if (data is None):
        return QImage()
    data = data.copy()
    data[(data > 255)] = 255
    data[(data < 0)] = 0
    data = data.astype(np.uint8)
    if (data.dtype == np.uint8):
        if (len(data.shape) == 2):
            qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_Indexed8)
            qim.setColorTable(gray_color_table)
            return (qim.copy() if copy else qim)
        elif (len(data.shape) == 3):
            if (data.shape[2] == 1):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_Grayscale8)
                return (qim.copy() if copy else qim)
            if (data.shape[2] == 3):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_RGB888)
                return (qim.copy() if copy else qim)
            elif (data.shape[2] == 4):
                qim = QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QImage.Format_ARGB32)
                return (qim.copy() if copy else qim)
            else:
                raise Exception(('Conversion of %d channel array to QImage not implemented' % data.shape[2]))
    raise Exception(('Conversion of %d dimension array to QImage not implemented' % len(data.shape)))