将图片某个区域批量填充白色(jsx代码)

// 定义源文件夹和目标文件夹路径
var sourceFolderPath = "C:/Users/***/Desktop/拆分/";
var destFolderPath = "C:/Users/***/Desktop/结果/";

// 定义要填充的区域坐标 (x, y, 宽度, 高度)
var fillArea = {
    x: 765,       // 左上角x坐标
    y: 66,       // 左上角y坐标
    width: 157,  // 区域宽度
    height: 24   // 区域高度
};

// 检查源文件夹是否存在
var sourceFolder = new Folder(sourceFolderPath);
if (!sourceFolder.exists) {
    alert("源文件夹不存在: " + sourceFolderPath);
    exit();
}

// 创建目标文件夹(如果不存在)
var destFolder = new Folder(destFolderPath);
if (!destFolder.exists) {
    destFolder.create();
}

// 获取源文件夹中的所有JPG文件
var files = sourceFolder.getFiles(/\.(jpg|jpeg)$/i);
if (files.length === 0) {
    alert("源文件夹中没有找到JPG文件");
    exit();
}

// 批量处理文件
var successCount = 0;
for (var i = 0; i < files.length; i++) {
    var file = files[i];
    if (processFile(file)) {
        successCount++;
    }
}

alert("处理完成!\n成功处理: " + successCount + " 个文件\n失败: " + (files.length - successCount) + " 个文件");

// 处理单个文件的函数
function processFile(file) {
    try {
        // 打开文件
        var doc = app.open(file);
        

        // 创建选框
        doc.selection.select([
            [fillArea.x, fillArea.y],
            [fillArea.x + fillArea.width, fillArea.y],
            [fillArea.x + fillArea.width, fillArea.y + fillArea.height],
            [fillArea.x, fillArea.y + fillArea.height]
        ]);
        
        // 设置前景色为白色
        var whiteColor = new SolidColor();
        whiteColor.rgb.red = 255;
        whiteColor.rgb.green = 255;
        whiteColor.rgb.blue = 255;
        app.foregroundColor = whiteColor;
        
        // 填充选框
        doc.selection.fill(app.foregroundColor);
        
        // 取消选择
        doc.selection.deselect();
        
        // 创建保存路径
        var destFile = new File(destFolderPath + file.name);
        
        // 保存到目标文件夹
        var saveOptions = new JPEGSaveOptions();
        saveOptions.quality = 12; // JPG质量,0-12(12为最高)
        doc.saveAs(destFile, saveOptions, true);
        doc.close(SaveOptions.DONOTSAVECHANGES);
        
        return true;
    } catch (e) {
        alert("处理文件 " + file.name + " 时出错: " + e.message);
        return false;
    }

}
posted @ 2025-10-01 13:19  python_learn  阅读(7)  评论(0)    收藏  举报