• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
1

Saving WEBp image by script

Community Beginner ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting , Windows

Views

6.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 11, 2023 Mar 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

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Thank you so much guys, I will try it!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

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
    }
}

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2024 Jul 22, 2024

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2024 Jul 22, 2024

Copy link to clipboard

Copied

quote

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?


By @musekic

 

The example code uses lossy compression @ 75% quality with all three kinds of metadata included.

 

Is this what is being used in the manual save that you say is half the size?

 

We need to compare apples to apples, otherwise, perhaps there is an issue with the script code or perhaps a bug...

 

Edit: Using the same compression and metadata settings with the same original image...

Manually saving = 1,037,826 bytes

Scripted saving = 1,037,830 bytes

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 24, 2024 Jul 24, 2024

Copy link to clipboard

Copied

You nailed it. It was the metadata. I don't know what the metadata options do for me so when saving manually I always leave them unselected. When saving them manually, selecting all the metadata options makes the difference in file sizes.

 

I tried commenting out the "Metadata options" in your script but it didn't change the resulting webp file sizes. Do you know how I'd edit your script so that it won't save metadata? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 24, 2024 Jul 24, 2024

Copy link to clipboard

Copied

@musekic 

 

Change te following 3 values from true to false:

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 24, 2024 Jul 24, 2024

Copy link to clipboard

Copied

OMG! I figured it out the same time I saw your post. Thanks again. 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 24, 2024 Jul 24, 2024

Copy link to clipboard

Copied

LATEST
quote

OMG! I figured it out the same time I saw your post. Thanks again. 🙂


By @musekic

 

That's great! Welcome to scripting.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 24, 2024 Jul 24, 2024

Copy link to clipboard

Copied

please ignore my last post - I'm so grateful to be able to use your code!

 

To NOT INCLUDE metadata and thus have smaller resulting webp file sizes...

I just had to change "true" values to "false" for each metadata type  (2 instances of this same code) >>>
saveWebP("compressionLossy", 75, false, false, false, true);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2023 Mar 21, 2023

Copy link to clipboard

Copied

Yes! It works! It needs a version 2022... Thank you so much! Petr

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

Thanks! If only this piece of code could be implemented in Image Processor Pro, that would be magic.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines