Photoshop批量处理脚本无法实现图片压缩的问题求助
Photoshop批量处理脚本无法实现图片压缩的问题求助
嘿,我帮你排查了下脚本的问题,发现几个关键原因导致压缩没生效,咱们一步步说清楚:
核心问题分析
setJPEGQuality函数未正确获取当前文档
你在createImages循环里定义的doc是局部变量,setJPEGQuality函数里直接用doc根本找不到这个对象!这就导致临时文件保存的步骤完全没执行,质量调整的逻辑等于白写。临时文件路径不兼容跨平台
你写的~/Desktop/temp.jpg在Windows系统下是无效路径,会导致临时文件创建失败,压缩逻辑直接中断,自然没法调整质量。未处理非RGB模式的文档
如果原文档是CMYK或者其他颜色模式,直接保存JPG可能无法正确应用压缩设置,甚至导致压缩效果完全不符合预期。
修正后的完整脚本
我把这些问题都修复了,你可以直接运行这个版本试试:
// variables var inputFolder = Folder.selectDialog("SELECT A FOLDER OF IMAGES TO RESIZE"); var outputFolder = inputFolder // check that the folder has files in it if (inputFolder != null) { var fileList = inputFolder.getFiles(/.+\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i); } else { alert("Couldn't find any files. Check your directory path and try again."); Exit(); } // set background color var BGcolor = new SolidColor(); BGcolor.rgb.red = 255; BGcolor.rgb.green = 255; BGcolor.rgb.blue = 255; // set your color as background color backgroundColor.rgb.hexValue = BGcolor.rgb.hexValue; // new image sizes var large = 900; var normal = 600; var thumbnail = 300; // new image name extentions var largeNameExt = '_l'; var normalNameExt = '_n'; var thumbnailNameExt = '_t'; // set jpeg save options var jpegSaveOptions = new JPEGSaveOptions(); jpegSaveOptions.embedColorProfile = true; jpegSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; jpegSaveOptions.matte = MatteType.NONE; jpegSaveOptions.quality = 12; // error logging var errors = []; /* Function: createFolder Description: creates the new folders for export imageSize: the new image size used to create the folder path */ function createFolders(imageSize) { var addFolder = new Folder(outputFolder + '/' + imageSize.toString() + 'px' + '/'); addFolder.create(); } // createFolders // Set jpeg save options with quality adjustments // 新增doc参数,传递当前操作的文档对象 function setJPEGQuality(doc, saveOptions, targetSizeKB) { var quality = 12; // Start with maximum quality // 使用跨平台的桌面路径生成临时文件 var tempFile = new File(Folder.desktop.fsName + "/temp.jpg"); do { saveOptions.quality = quality; // 使用传入的doc对象保存临时文件 doc.saveAs(tempFile, saveOptions, true, Extension.LOWERCASE); var fileSize = tempFile.length / 1024; // Convert bytes to KB if (fileSize > targetSizeKB) { quality -= 1; // Reduce quality to decrease file size } else { break; // Exit the loop if target size is met } } while (quality > 0); tempFile.remove(); // Clean up the temp file } // CreateImages function function createImages() { // add new folder paths createFolders(large); createFolders(normal); createFolders(thumbnail); // trim the images then resize for (var i = 0; i < fileList.length; i++) { // open the image try { var doc = app.open(fileList[i]); // 强制转换为RGB模式,确保JPG压缩设置正常生效 if (doc.mode !== DocumentMode.RGB) { doc.changeMode(ChangeMode.RGB); } // set the output locations and names var fileName = fileList[i].name.replace(/\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/, ''); var saveImgLarge = new File(outputFolder + "/" + large.toString() + 'px' + '/' + fileName + largeNameExt + '.jpg'); var saveImgNormal = new File(outputFolder + "/" + normal.toString() + 'px' + '/' + fileName + normalNameExt + '.jpg'); var saveImgThumbnail = new File(outputFolder + "/" + thumbnail.toString() + 'px' + '/' + fileName + thumbnailNameExt + '.jpg'); // trim whitespace doc.trim(); // Resize and save images // Large if (doc.height > doc.width) { doc.resizeImage(null, UnitValue(large, "px"), 72, ResampleMethod.BICUBICAUTOMATIC); } else { doc.resizeImage(UnitValue(large, "px"), null, 72, ResampleMethod.BICUBICAUTOMATIC); } doc.resizeCanvas(large, large, AnchorPosition.MIDDLECENTER); // 传递当前doc对象给压缩函数 setJPEGQuality(doc, jpegSaveOptions, 70); // Adjust quality to target size 70 KB doc.saveAs(saveImgLarge, jpegSaveOptions, true, Extension.LOWERCASE); // Normal doc.resizeImage(UnitValue(normal, "px"), UnitValue(normal, "px"), 72, ResampleMethod.BICUBICAUTOMATIC); setJPEGQuality(doc, jpegSaveOptions, 50); // Adjust quality to target size 50 KB doc.saveAs(saveImgNormal, jpegSaveOptions, true, Extension.LOWERCASE); // Thumbnail doc.resizeImage(UnitValue(thumbnail, "px"), UnitValue(thumbnail, "px"), 72, ResampleMethod.BICUBICAUTOMATIC); setJPEGQuality(doc, jpegSaveOptions, 20); // Adjust quality to target size 20 KB doc.saveAs(saveImgThumbnail, jpegSaveOptions, true, Extension.LOWERCASE); doc.close(SaveOptions.DONOTSAVECHANGES); } catch (err) { errors.push(fileList[i].name); } } } createImages(); if (errors.length > 0) { alert("Oops! We ran into some errors.\nWhile the program did run successfully, you will still need to double-check the files listed below for issues like these:" + "\n\n1) Files are corrupt (files don't open, or cause an error when you try to open them)\n2) Files have unusual characters in their file names\n3) Files are not one of these types of image files:\ngif, jpeg, jpg, eps, tiff, tif, psd, pdf, bmp, png" + "\n\n*** FILE ERRORS ***\n" + errors.join("\n")); } else { alert("Task Completed!"); }
关键修改说明
- 传递文档对象:把当前打开的
doc作为参数传给setJPEGQuality函数,确保压缩逻辑能操作到正确的图片。 - 跨平台路径:用
Folder.desktop.fsName获取系统桌面路径,避免Windows和Mac的路径格式冲突。 - 颜色模式转换:打开文档后自动转为RGB模式,保证JPG的压缩设置能正常生效。
运行这个脚本后,应该就能同时完成尺寸调整和图片压缩了!如果还有个别特殊格式的图片出现问题,可以再排查下文件本身的兼容性哦~
备注:内容来源于stack exchange,提问作者Decent




