1

我有一个脚本试图:

  • 扫描 InDesign 文档中的所有图像
  • BridgeTalk  通过对象将所有图像发送到 Photoshop  
  • 将所有图像的大小调整为 600px 宽度(在数学上保持纵横比)
  • 将所有图像从 Photoshop 导出到新文件夹

似乎我可能需要以编程方式调整每张图像的 DPI,因为在调整一张图像大小之前 Photoshop 就崩溃了。该错误表明此脚本使临时内存过载,我认为它与图像质量和/或大小有关...这是错误消息:

发生一般 Photoshop 错误。此功能在此版本的 Photoshop 中可能不可用。
第 1 行中的错误:
由于暂存盘已满,无法完成命令。




这是转换图像大小的相关代码:

function resaveInPS(imagePaths, imagesFolder)
{
    /*
     * NOTE:  no single-line comments are allowed in this function, because it is not read line-by-line by BridgeTalk, but as a single String;
     *        only multi-line comments will work, because they are terminated at each end
     */

    BridgeTalk.bringToFront("photoshop"); /* switch view from InDesign to Photoshop */

    app.displayDialogs = DialogModes.NO; /* Photoshop statement, prevents status alerts from interrupting */

    var imagePath = "";
    var fileName = "";
    var largerImage = "";

    for(var i = 0; i < imagePaths.length; i++)
    {
        imagePath = imagePaths[i].fullName;
        fileName = imagePaths[i].name;
        largerImage = fileName.substr(0, fileName.length - 4); /* getting rid of the file extension:  Photoshop will handle the file extension */

        var photoshopDoc = "";
        photoshopDoc = app.open(new File(imagePath) );

        var currentWidth = photoshopDoc.width; /* in inches */
        var currentHeight = photoshopDoc.height; /* in inches */

        currentWidth.convert("px"); /* now in pixels */
        currentHeight.convert("px"); /* now in pixels */

        var newWidth = 600; /* defining the desired exported image width here */
        var ratio = newWidth / currentWidth;
        var newHeight = ratio * currentHeight; /* maintaining aspect ratio of the resized image's height here */

        alert("The currentHeight is " + currentHeight + ".\n\nThe ratio is " + ratio + ".\n\nThe newHeight is " + newHeight + ".");

        photoshopDoc.resizeImage(newHeight, newWidth); /* (height, width) */
        photoshopDoc.resizeCanvas(newHeight, newWidth); /* (height, width) */

        var saveOptions = new TiffSaveOptions(); /* handling the file extension here */
        photoshopDoc.saveAs(new File(imagesFolder + "/" + largerImage), saveOptions); /* saving the new image in the folder here, with the file extension */
        photoshopDoc.close(SaveOptions.DONOTSAVECHANGES); /* close the Photoshop document without saving */
        app.purge(PurgeTarget.ALLCACHES); /* clears the clipboard, history, and undo cache in Photoshop; Note: does NOT delete the temporary files! */

    } /* end of for loop */

    app.displayDialogs = DialogModes.ALL; /* resume normal dialogs after saving the file and closing the document */
    app.purge(PurgeTarget.ALLCACHES); /* clears the clipboard, history and undo cache in Photoshop; Note:  does NOT delete the temporary files! */

} // end of function ResaveInPS


注意  ——我对该语句的使用  app.purge(PurgeTarget.ALLCACHES)  似乎没有太大影响,因为错误仍在发生......

4

1 回答 1

2

所以,我错误地假设类型强制将适用于单位值,但当然它不会因为“像素”不是默认的 Javascript 类型。

如果不应用  new UnitValue  to   var ratio,则将 ratio 强制转换为类型  Number  (尽管  使用“像素”单元类型实例化)currentWidth  此外,  它var newWidth  仍然是一种类型  Number


这是更正后的代码,从该行开始  var currentWidth  并以 结尾  resizeCanvas :

        var currentWidth = photoshopDoc.width; /* in inches */
        var currentHeight = photoshopDoc.height; /* in inches */

        currentWidth.convert("px"); /* now in pixels */
        currentHeight.convert("px"); /* now in pixels */


        var newWidth = new UnitValue(600, "px"); /* defining the desired exported image width here */
        var ratio = new UnitValue(newWidth / currentWidth, "px");
        var newHeight = new UnitValue(ratio * currentHeight, "px"); /* maintaining aspect ratio of the resized image's height here */

        /*alert("The currentHeight is " + currentHeight + ".\n\nThe ratio is " + ratio + ".\n\nThe newHeight is " + newHeight + ".");*/

        photoshopDoc.resizeImage(newWidth, newHeight); /* (width, height) */
        photoshopDoc.resizeCanvas(newWidth, newHeight); /* (width, height) */


另外,我得到了方法的参数  resizeImage  并  resizeCanvas  倒退了!这是两者的正确顺序:   (width, height)


-- 感谢@Mark 为我指明了正确的方向。

于 2012-07-16T20:10:30.560 回答