QT使用QImage制作图片的四种(圆形,六边形,复古与负片)效果(测试过效果的代码)

负片效果:

QImage originalImage("path/to/image.jpg");

// 对每个像素进行操作
for (int y = 0; y < originalImage.height(); y++) {
    for (int x = 0; x < originalImage.width(); x++) {
        QColor color = originalImage.pixelColor(x, y);

        // 颜色反转
        int red = 255 - color.red();
        int green = 255 - color.green();
        int blue = 255 - color.blue();

        // 设置新的颜色
        QColor newColor(red, green, blue);
        originalImage.setPixelColor(x, y, newColor);
    }
}

originalImage.save("path/to/negative_image.jpg");

复古效果: 

QImage originalImage("path/to/image.jpg");

// 复古效果参数
int hueShift = 30;       // 色调偏移量
int saturationShift = -50;  // 饱和度偏移量
int contrastLevel = 30;  // 对比度水平

// 对每个像素进行操作
for (int y = 0; y < originalImage.height(); y++) {
    for (int x = 0; x < originalImage.width(); x++) {
        QColor color = originalImage.pixelColor(x, y);

        // 调整色调
        int hue = color.hue() + hueShift;
        if (hue < 0) {
            hue += 360;
        } else if (hue >= 360) {
            hue -= 360;
        }

        // 调整饱和度
        int saturation = qMax(0, qMin(color.saturation() + saturationShift, 255));

        // 调整亮度
        int value = color.value();

        // 调整对比度
        int contrast = value < 128 ? contrastLevel : -contrastLevel;
        value = qMax(0, qMin(value + contrast, 255));

        // 设置新的颜色
        QColor newColor;
        newColor.setHsv(hue, saturation, value);
        originalImage.setPixelColor(x, y, newColor);
    }
}

originalImage.save("path/to/vintage_image.jpg");

 裁剪成圆形

 

QImage rotatedImage("path/to/image.jpg");      
QImage resultImage(rotatedImage.size(), QImage::Format_ARGB32);
resultImage.fill(Qt::transparent);

QPainter painter(&resultImage);
painter.setRenderHint(QPainter::Antialiasing);

 // 创建一个圆形路径,并将其设置为绘制区域
QPainterPath path;
path.addEllipse(resultImage.rect());
painter.setClipPath(path);
painter.setClipping(true);

// 在绘制区域内绘制原始图像
painter.drawImage(resultImage.rect(), rotatedImage);
painter.end();

rotatedImage.save("path/to/vintage_image.jpg");

裁剪成六边形:

        QImage rotatedImage("path/to/image.jpg");    

        QImage mask(rotatedImage.size(), QImage::Format_ARGB32);
        mask.fill(Qt::transparent);
        QPainter maskPainter(&mask);
        maskPainter.setRenderHint(QPainter::Antialiasing);
        maskPainter.setPen(Qt::NoPen);
        maskPainter.setBrush(Qt::black);

        // 构建六边形
        QPolygonF hexagon;
        int centerX = mask.width() / 2;
        int centerY = mask.height() / 2;
        int sideLength = std::min(mask.width(), mask.height()) / 2;
        for (int i = 0; i < 6; i++) {
            qreal angle = (60.0 * i + 30.0) * M_PI / 180.0;
            QPointF point(centerX + sideLength * cos(angle), centerY + sideLength * sin(angle));
            hexagon << point;
        }
        // 绘制六边形遮罩
        maskPainter.drawPolygon(hexagon);
        // 应用遮罩到原始图像
        rotatedImage.setAlphaChannel(mask.alphaChannel());
        rotatedImage.save("path/to/vintage_image.jpg");

 

你可能感兴趣的:(qt)