Skip to main content
Participant
May 15, 2024
Answered

The JPG image file size is too large after exporting a PSD file to JPG using ExtendScript

  • May 15, 2024
  • 2 replies
  • 439 views
When I export a PSD file to a JPG using the Photoshop client, the file size is only a few tens of kilobytes when the quality is set to 7. However, when I use ExtendScript's SaveAs function, the resulting file size is around 3-4 megabytes. Even when dynamically adjusting the quality parameter, the reduction in size is very limited. Below is my code, please help me find a solution.
 
        finalPsdFile = new File(folder + "/" + imageName + "/" + makeName + "/" + outName + ".psd");
        docRef = app.open(finalPsdFile);
 
        var jpgFile = new File(folder + "/" + imageName + "/" + makeName + "/" + outName + ".jpg");
        var jpgOptions = new JPEGSaveOptions();
        jpgOptions.matte = MatteType.NONE;
        jpgOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
        jpgOptions.embedColorProfile = true;
        quality = 12
        jpgOptions.quality = quality;
        docRef.saveAs(jpgFile, jpgOptions, true, Extension.LOWERCASE);
 
        var maxFileSize = makeData["maxSize"];
        while (maxFileSize > 0 && jpgFile.length > maxFileSize * 1024 && quality > 0) {
            quality -= 1;
            jpgOptions.quality = quality;
            docRef.saveAs(jpgFile, jpgOptions, true, Extension.LOWERCASE);
            jpgFile.close();
            jpgFile = new File(folder + "/" + imageName + "/" + makeName + "/"  + outName + ".jpg");
        }

 

        docRef.close(SaveOptions.DONOTSAVECHANGES);
This topic has been closed for replies.
Correct answer Stephen Marsh

If this is metadata related bloat, you can script Export > Save for Web which can exclude metadata... Or if using Save As which includes metadata you can find examples of removing 3 different types of metadata in the following script where I was saving to JPEG:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-both-export-and-resize-layer-comps/td-p/14614232

 

Another option is to duplicate all layers to a new doc which will also exclude document-level metadata.

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 15, 2024

If this is metadata related bloat, you can script Export > Save for Web which can exclude metadata... Or if using Save As which includes metadata you can find examples of removing 3 different types of metadata in the following script where I was saving to JPEG:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-both-export-and-resize-layer-comps/td-p/14614232

 

Another option is to duplicate all layers to a new doc which will also exclude document-level metadata.

Participant
May 15, 2024

Thank you very much. I was able to successfully eliminate the impact of the metadata mentioned earlier by using the 'Save for Web' option, and the file size is now normal.

Stephen Marsh
Community Expert
Community Expert
May 15, 2024

@S37409051iybg 

 

You’re welcome. Perhaps one day Adobe will allow Export As to be recorded into actions and be accessible via UXP scripting. Until then, Export: Save for Web (Legacy) is the fallback.

Stephen Marsh
Community Expert
Community Expert
May 15, 2024

I'm guessing that this is due to metadata, most likely excessive photoshop:DocumentAncestors metadata:

 

https://prepression.blogspot.com/2017/06/metadata-bloat-photoshopdocumentancestors.html