Skip to main content
Participant
March 11, 2023
Answered

Saving WEBp image by script

  • March 11, 2023
  • 2 replies
  • 9578 views

I use this script images in *.jpg format:

 

var saveFolder = new Folder('/c/Users/petr/Desktop');
var fileName = doc.name.split('.')[0];

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 5;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
jpgOptions.scans = 5;
jpgOptions.matte = MatteType.NONE;

doc.saveAs (new File(saveFolder + '/' + fileName + '.jpg'), jpgOptions);

 

Now I need to use it for WEBp format and I'm not able to find a correct version of webp options to use them in doc.save command... Any idea, please?

Correct answer Stephen Marsh

As far as I know, the DOM hasn't been updated since 2020 to include this v2022 feature, so Action Manager code is needed.

 

The code in the previously referenced topic was a quick one-off, perhaps not as "reusable" as it could be.

 

I have cleaned up the code, added it to a function and offered some features to help make it general-purpose. Double-check the comments in the code to change the various options, hopefully, this is clear enough, but if not just let me know.

 

Edit #2: Code updated to a 1.3 version!

 

 

saveWebP("compressionLossy", 75, true, true, true, false);

function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
    /*
    v1.3 - 10th February 2024, Stephen Marsh
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
    */

    // Ensure that version 2022 or later is being used
    var versionNumber = app.version.split(".");
    var versionCheck = parseInt(versionNumber);
    // Fail
    if (versionCheck < 23) {
        alert("You must use Photoshop 2022 or later to save using native WebP format...");
    // Pass
    } else {
        // Doc and path save variables
        var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
        var WebPSavePath = "~/Desktop" + "/" + WebPDocName + ".webp" // Change path as needed
        var WebPFile = new File(WebPSavePath); // Create the file object
        
        /*
        // Check for existing file object
        if (WebPFile.exists) {
            // true = 'No' as default active button
            if (!confirm("File exists, overwrite: Yes or No?", true))
                // throw alert("Script cancelled!");
                throw null;
        }
        */

        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();

        // Compression parameters = "compressionLossless" | "compressionLossy"
        descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
        var WebPCompIsLossless = false; // set the default flag for compression
        if (WebPCompIsLossless == false) {
            // 0 (lowest lossy quality) - 100 (highest lossy quality)
            descriptor2.putInteger(s2t("quality"), compValue); //  number variable
        }
        
        // Metadata options
        descriptor2.putBoolean(s2t("includeXMPData"), xmpData); // Boolean param moved to function call
        descriptor2.putBoolean(s2t("includeEXIFData"), exifData); // Boolean param moved to function call
        descriptor2.putBoolean(s2t("includePsExtras"), psData); // Boolean param moved to function call
        
        // WebP format and save path
        descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
        descriptor.putPath(s2t("in"), WebPFile); // Save path variable
        
        // Save As = false | Save As a Copy = true
        descriptor.putBoolean(s2t("copy"), asCopy); // Boolean param moved to function call
        
        // The extension
        descriptor.putBoolean(s2t("lowerCase"), true);

        // If the doc isn't in RGB mode, convert to sRGB
        if (activeDocument.mode !== DocumentMode.RGB) {
            activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
            activeDocument.changeMode(ChangeMode.RGB);
        }

        // If the doc is in RGB mode, convert to sRGB
        //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);

        // Convert to 8 bpc
        activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;
        
        // Execute the save
        executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog
    }
}

 

 

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
March 11, 2023

As far as I know, the DOM hasn't been updated since 2020 to include this v2022 feature, so Action Manager code is needed.

 

The code in the previously referenced topic was a quick one-off, perhaps not as "reusable" as it could be.

 

I have cleaned up the code, added it to a function and offered some features to help make it general-purpose. Double-check the comments in the code to change the various options, hopefully, this is clear enough, but if not just let me know.

 

Edit #2: Code updated to a 1.3 version!

 

 

saveWebP("compressionLossy", 75, true, true, true, false);

function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
    /*
    v1.3 - 10th February 2024, Stephen Marsh
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
    */

    // Ensure that version 2022 or later is being used
    var versionNumber = app.version.split(".");
    var versionCheck = parseInt(versionNumber);
    // Fail
    if (versionCheck < 23) {
        alert("You must use Photoshop 2022 or later to save using native WebP format...");
    // Pass
    } else {
        // Doc and path save variables
        var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
        var WebPSavePath = "~/Desktop" + "/" + WebPDocName + ".webp" // Change path as needed
        var WebPFile = new File(WebPSavePath); // Create the file object
        
        /*
        // Check for existing file object
        if (WebPFile.exists) {
            // true = 'No' as default active button
            if (!confirm("File exists, overwrite: Yes or No?", true))
                // throw alert("Script cancelled!");
                throw null;
        }
        */

        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();

        // Compression parameters = "compressionLossless" | "compressionLossy"
        descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
        var WebPCompIsLossless = false; // set the default flag for compression
        if (WebPCompIsLossless == false) {
            // 0 (lowest lossy quality) - 100 (highest lossy quality)
            descriptor2.putInteger(s2t("quality"), compValue); //  number variable
        }
        
        // Metadata options
        descriptor2.putBoolean(s2t("includeXMPData"), xmpData); // Boolean param moved to function call
        descriptor2.putBoolean(s2t("includeEXIFData"), exifData); // Boolean param moved to function call
        descriptor2.putBoolean(s2t("includePsExtras"), psData); // Boolean param moved to function call
        
        // WebP format and save path
        descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
        descriptor.putPath(s2t("in"), WebPFile); // Save path variable
        
        // Save As = false | Save As a Copy = true
        descriptor.putBoolean(s2t("copy"), asCopy); // Boolean param moved to function call
        
        // The extension
        descriptor.putBoolean(s2t("lowerCase"), true);

        // If the doc isn't in RGB mode, convert to sRGB
        if (activeDocument.mode !== DocumentMode.RGB) {
            activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
            activeDocument.changeMode(ChangeMode.RGB);
        }

        // If the doc is in RGB mode, convert to sRGB
        //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);

        // Convert to 8 bpc
        activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;
        
        // Execute the save
        executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog
    }
}

 

 

 

Stephen Marsh
Community Expert
Community Expert
March 14, 2023
Inspiring
July 23, 2024

the script works like a dream but the saved WebP files are about double the size as when I save them manually. I see this is an old post but reaching out anyway - any suggestions on how to reduce the webp file sizes?

Stephen Marsh
Community Expert
Community Expert
March 11, 2023

I have some example code here:

 

 
I am presuming that you are looking for the native save as WebP that was introduced in v2022 (and not an earlier released third-party WebP plug-in).
Petr5DB5Author
Participant
March 13, 2023
Thank you so much guys, I will try it!