当前位置:   article > 正文

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

qimage 画圆

负片效果:

  1. QImage originalImage("path/to/image.jpg");
  2. // 对每个像素进行操作
  3. for (int y = 0; y < originalImage.height(); y++) {
  4. for (int x = 0; x < originalImage.width(); x++) {
  5. QColor color = originalImage.pixelColor(x, y);
  6. // 颜色反转
  7. int red = 255 - color.red();
  8. int green = 255 - color.green();
  9. int blue = 255 - color.blue();
  10. // 设置新的颜色
  11. QColor newColor(red, green, blue);
  12. originalImage.setPixelColor(x, y, newColor);
  13. }
  14. }
  15. originalImage.save("path/to/negative_image.jpg");

复古效果: 

  1. QImage originalImage("path/to/image.jpg");
  2. // 复古效果参数
  3. int hueShift = 30; // 色调偏移量
  4. int saturationShift = -50; // 饱和度偏移量
  5. int contrastLevel = 30; // 对比度水平
  6. // 对每个像素进行操作
  7. for (int y = 0; y < originalImage.height(); y++) {
  8. for (int x = 0; x < originalImage.width(); x++) {
  9. QColor color = originalImage.pixelColor(x, y);
  10. // 调整色调
  11. int hue = color.hue() + hueShift;
  12. if (hue < 0) {
  13. hue += 360;
  14. } else if (hue >= 360) {
  15. hue -= 360;
  16. }
  17. // 调整饱和度
  18. int saturation = qMax(0, qMin(color.saturation() + saturationShift, 255));
  19. // 调整亮度
  20. int value = color.value();
  21. // 调整对比度
  22. int contrast = value < 128 ? contrastLevel : -contrastLevel;
  23. value = qMax(0, qMin(value + contrast, 255));
  24. // 设置新的颜色
  25. QColor newColor;
  26. newColor.setHsv(hue, saturation, value);
  27. originalImage.setPixelColor(x, y, newColor);
  28. }
  29. }
  30. originalImage.save("path/to/vintage_image.jpg");

 裁剪成圆形

 

  1. QImage rotatedImage("path/to/image.jpg");
  2. QImage resultImage(rotatedImage.size(), QImage::Format_ARGB32);
  3. resultImage.fill(Qt::transparent);
  4. QPainter painter(&resultImage);
  5. painter.setRenderHint(QPainter::Antialiasing);
  6. // 创建一个圆形路径,并将其设置为绘制区域
  7. QPainterPath path;
  8. path.addEllipse(resultImage.rect());
  9. painter.setClipPath(path);
  10. painter.setClipping(true);
  11. // 在绘制区域内绘制原始图像
  12. painter.drawImage(resultImage.rect(), rotatedImage);
  13. painter.end();
  14. rotatedImage.save("path/to/vintage_image.jpg");

裁剪成六边形:

  1. QImage rotatedImage("path/to/image.jpg");
  2. QImage mask(rotatedImage.size(), QImage::Format_ARGB32);
  3. mask.fill(Qt::transparent);
  4. QPainter maskPainter(&mask);
  5. maskPainter.setRenderHint(QPainter::Antialiasing);
  6. maskPainter.setPen(Qt::NoPen);
  7. maskPainter.setBrush(Qt::black);
  8. // 构建六边形
  9. QPolygonF hexagon;
  10. int centerX = mask.width() / 2;
  11. int centerY = mask.height() / 2;
  12. int sideLength = std::min(mask.width(), mask.height()) / 2;
  13. for (int i = 0; i < 6; i++) {
  14. qreal angle = (60.0 * i + 30.0) * M_PI / 180.0;
  15. QPointF point(centerX + sideLength * cos(angle), centerY + sideLength * sin(angle));
  16. hexagon << point;
  17. }
  18. // 绘制六边形遮罩
  19. maskPainter.drawPolygon(hexagon);
  20. // 应用遮罩到原始图像
  21. rotatedImage.setAlphaChannel(mask.alphaChannel());
  22. rotatedImage.save("path/to/vintage_image.jpg");

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/182018
推荐阅读
相关标签
  

闽ICP备14008679号