# Photoshop V26.7.0 自动扣图脚本(扣出衣服) 以下是一个使用Photoshop JavaScript脚本自动扣图的代码,主要基于选择工具和蒙版技术来提取衣服部分: ```javascript // Photoshop自动扣衣服脚本 // 适用于Photoshop V26.7.0 // 确保有文档打开 if (app.documents.length > 0) { var doc = app.activeDocument; try { // 保存当前首选项设置 var savedRulerUnits = app.preferences.rulerUnits; var savedTypeUnits = app.preferences.typeUnits; app.preferences.rulerUnits = Units.PIXELS; app.preferences.typeUnits = TypeUnits.PIXELS; // 激活图层(如果存在) if (doc.activeLayer.isBackgroundLayer) { doc.activeLayer.isBackgroundLayer = false; } // 复制当前图层作为工作层 var originalLayer = doc.activeLayer; var workingLayer = originalLayer.duplicate(); workingLayer.name = "Working Layer"; // 使用选择主体功能自动选择主要对象(通常是衣服) var selection = null; try { // Photoshop V26.7.0的选择主体功能 doc.selection.selectSubject(); selection = doc.selection; // 如果自动选择不理想,可以添加手动调整步骤 if (selection.bounds) { // 羽化选区使边缘更自然 doc.selection.feather(2); // 创建图层蒙版 workingLayer.addMask(); // 反转选区并填充黑色(隐藏非衣服部分) doc.selection.invert(); var fillColor = new SolidColor(); fillColor.rgb.red = 0; fillColor.rgb.green = 0; fillColor.rgb.blue = 0; doc.selection.fill(fillColor); doc.selection.deselect(); // 可选:使用调整图层优化结果 var levelsLayer = doc.artLayers.add(); levelsLayer.name = "Levels Adjustment"; levelsLayer.kind = LayerKind.ADJUSTMENT; var levelsRef = new ActionReference(); levelsRef.putClass(charIDToTypeID("Lvls")); var levelsDesc = new ActionDescriptor(); levelsDesc.putReference(charIDToTypeID("null"), levelsRef); executeAction(charIDToTypeID("Mk "), levelsDesc, DialogModes.NO); // 合并可见图层(可选) // doc.mergeVisibleLayers(); } else { alert("自动选择失败,请尝试手动选择"); } } catch (e) { alert("选择主体功能出错: " + e.message); } // 恢复首选项设置 app.preferences.rulerUnits = savedRulerUnits; app.preferences.typeUnits = savedTypeUnits; } catch (e) { alert("脚本执行出错: " + e.message); } } else { alert("没有打开的文档"); } ``` ## 使用说明 1. 打开包含衣服的图片 2. 在Photoshop中通过"文件 > 脚本 > 浏览"运行此脚本 3. 脚本会自动尝试选择衣服主体 4. 如果自动选择不理想,可以手动调整选区后再运行脚本 ## 注意事项 1. 自动扣图效果取决于图片质量,复杂可能需要手动调整 2. 对于半透明衣物,可能需要额外处理透明度通道 3. 高对比度的图片通常能获得更好的自动选择结果 ## 额外逻辑点 1. 脚本使用了Photoshop V26.7.0的`selectSubject()` API,这是Adobe Sensei提供的AI功能 2. 羽化参数(2像素)可根据实际图片分辨率调整 3. 脚本保留了原始图层,确保可以随时回退修改 [2025-06-06 01:46:10 | AI写代码神器 | 843点数解答]