负片效果:
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");
裁剪成圆形
文章来源:https://www.toymoban.com/news/detail-638819.html
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");
文章来源地址https://www.toymoban.com/news/detail-638819.html
到了这里,关于QT使用QImage制作图片的四种(圆形,六边形,复古与负片)效果(测试过效果的代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!