Qt 将图片白色背景转换为透明背景

QImage convertImageWhiteColorAlpha(QImage val)
{
    QImage imageAlpha = val.convertToFormat(QImage::Format_RGBA8888_Premultiplied, Qt::NoFormatConversion);
    int nWidth = imageAlpha.width();
    int nHeight = imageAlpha.height();

    for (int nCol = 0; nCol < nWidth; ++nCol)
    {
        for (int nRow = 0; nRow < nHeight; ++nRow)
        {
            QColor pixelColor = imageAlpha.pixelColor(nCol, nRow);

            // 如果该像素点的各分量值均大于240则认为该像素点的颜色为白色
            if (pixelColor.red() > 240 && pixelColor.green() > 240 && pixelColor.blue() > 240)
            {
                pixelColor.setAlpha(0);
                imageAlpha.setPixelColor(nCol, nRow, pixelColor);
            }
        }
    }

    return imageAlpha;
}

 

你可能感兴趣的:(Qt编程)