QT 实现颜色反转
在这里插入代码片
/*
颜色反转
imagePath :源图片的路径
newPixmap :新的图片会在源图片名后面增加“_new” 为图片名,
如果要考虑原来是否有这个文件的各种情况得再做特殊处理
*/
void MainWindow::imagesColorInverse(QString imagePath)
{
QPixmap sourcePixmap(imagePath);
QImage image = sourcePixmap.toImage();
for(int w = 0;w < image.width();++w)
for(int h = 0; h < image.height();++h)
{
QColor rgb = image.pixel(w,h);
rgb.setRgb(255-rgb.red(),255-rgb.green(),255 - rgb.blue());
///替换颜色
image.setPixel(w,h,rgb.rgba());
}
QPixmap newPixmap(QPixmap::fromImage(image));
QString newPath =imagePath;
newPath.insert(newPath.length()-4,"_new");
newPixmap.save(newPath);
}