qt – 如何将QImage裁剪到其不透明区域?

如果我有一个带有alpha通道的Q
Image,我怎样才能创建一个新的QImage,它被裁剪到不透明区域的边界框? 最佳答案 我找到了另一个SO答案(在C中)这样做:

Does Qt have a way to find bounding box of an image?

def bbox(p):
    bounding-box-of-an-image
    l = p.width()
    t = p.height()
    r = 0
    b = 0

    for y in range(p.height()):
        rowFilled = False
        for x in range(p.width()):
            if qAlpha(p.pixel(x, y)):
                rowFilled = True
                r = max(r, x)
                if l > x:
                    l = x
        if rowFilled:
            t = min(t, y)
            b = y
    return QRect(QPoint(l, t), QPoint(r, b))

但如果有更好/更快的方法来做这件事会很棒.

点赞