1. #pragma execution_character_set("utf-8")
    2. #include <QtCore/QCoreApplication>
    3. #include <QImage>
    4. #include <QRgb>
    5. #include <QVector>
    6. #include <QFile>
    7. #include <QDir>
    8. #include <QFileInfo>
    9. #include <QDebug>
    10. #include <Windows.h>
    11. bool isPixFit(QRgb color1, QRgb color2)
    12. {
    13. int deltaR = qAbs<int>(qRed(color1) - qRed(color2));
    14. int deltaG = qAbs<int>(qGreen(color1) - qGreen(color2));
    15. int deltaB = qAbs<int>(qBlue(color1) - qBlue(color2));
    16. if (deltaR < 0x10 && deltaG < 0x10 && deltaB < 0x10)
    17. return TRUE;
    18. return FALSE;
    19. }
    20. int main(int argc, char *argv[])
    21. {
    22. QCoreApplication a(argc, argv);
    23. QDir dir("C:\\Users\\16481\\Desktop\\pt");
    24. for (size_t i = 0; i < dir.count(); i++) {
    25. QString fileFull = dir[i]; // 文件全称
    26. QFileInfo fileInfo(dir, fileFull);
    27. QString fileSuffix = fileInfo.suffix();
    28. if (fileSuffix == "bmp") {
    29. QImage img(dir.absoluteFilePath(fileFull));
    30. QRgb rgb = QRgb(0x7fb0b3);
    31. // 把相似的颜色统一改为0x7fb0b3
    32. for (int x = 0; x < img.width(); x++) {
    33. for (int y = 0; y < img.height(); y++) {
    34. if (isPixFit(img.pixel(x, y), rgb)) {
    35. img.setPixel(x, y, rgb);
    36. }
    37. }
    38. }
    39. // 把图片4个角也改为0x7fb0b3,使之被视为透明图
    40. img.setPixel(0, 0, rgb);
    41. img.setPixel(0, img.width() - 1, rgb);
    42. img.setPixel(img.height() - 1, 0, rgb);
    43. img.setPixel(img.height() - 1, img.width() - 1, rgb);
    44. // 保存文件
    45. img.save(dir.absoluteFilePath(fileFull));
    46. qDebug() << fileFull + QString(" process finished!");
    47. }
    48. }
    49. return a.exec();
    50. }