misc.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:pcbre 作者: pcbre 项目源码 文件源码
def QImage_from_numpy(arr):
    shape = arr.shape

    if len(shape) == 2:
        channels = 1
    elif len(shape) == 3:
        channels = shape[3]
        shape = shape[:2]
    else:
        raise ValueError("Shape of input array is whacko")

    assert channels in [1,3,4]
    assert arr.dtype == numpy.uint8

    if channels != 4:
        new_arr = numpy.zeros(tuple(shape) + (4,), dtype = numpy.uint8)

        if channels > 1:
            new_arr[:,:,:channels] = arr
        else:
            new_arr[:,:,:3] = arr[:,:, numpy.newaxis]

        new_arr[:,:,3].fill(0xFF)

        arr = new_arr

    img = QtGui.QImage(arr.data, shape[1], shape[0], QtGui.QImage.Format_ARGB32)
    img.array_holder = arr

    return img
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号