Skip to main content
Participant
November 4, 2024
Answered

Update script to remove background and save as webp file with 60 quality

  • November 4, 2024
  • 2 replies
  • 773 views

Hi There,
I promise ive searched the community but have limited experience with scripts. =(

We have a simple script written for us by a previous colleague who pointed to a folder, opened - and resized all the images to be 1000 x 1000 pixels then saved that file to a resized folder.
We have updated requirements which means we would like to remove a white or grey background (if possible) and save the file as .webp with a quality of 60 in the same subfolder.
Is this possible? Am I asking too much!? i did try some simple command changes but obv i don't understand it enough
Script below
------------------

// To use this script, place it in Program Files\Adobe\Adobe Photoshop CS6\Presets\Scripts\ and run it from the Scripts option under File in Photoshop.
 
startRulerUnits = app.preferences.rulerUnits
app.preferences.rulerUnits =Units.PIXELS
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;
alert("This script only works with .jpg files! It will not process any images that are .png, .gif, .tif etc. Convert any images that are the wrong format first using the Image Processor script in Photoshop.");
var inputFolder = Folder.selectDialog("Select a folder to process");
var fileList = inputFolder.getFiles("*.JPG");
for(var i=0; i<fileList.length; i++) {
   var doc = open(fileList[i]);
doc.trim (TrimType.TOPLEFT)
if (doc.width !== doc.height) {
    if (doc.width > doc.height) {
        doc.resizeCanvas(doc.width, doc.width)
    } else {
        doc.resizeCanvas(doc.height, doc.height)
    }
}
doc.resizeImage(1000, 1000)
doc.changeMode(ChangeMode.RGB);
doc.save();
doc.close();
}
alert("Processing complete! Now all you've got to do is upload them all... :'(");


------

Thanks for taking the time to review.

This topic has been closed for replies.
Correct answer Stephen Marsh

Another ready-made option is to batch remove the background and resize, saving to transparent PNG using this script:

 

https://www.marspremedia.com/software/photoshop/batch-web-images

 

Then you can run my WebP Batch Processor script over the PNG files as a second pass. Although this intermediate PNG workflow is 2 step, depending on your requirements for transparency, you may not need to create an action to remove the background (unknown).

 

Also, it would help to provide before-and-after images demonstrating sizing and positioning for both white and grey backgrounds.

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 4, 2024

Another ready-made option is to batch remove the background and resize, saving to transparent PNG using this script:

 

https://www.marspremedia.com/software/photoshop/batch-web-images

 

Then you can run my WebP Batch Processor script over the PNG files as a second pass. Although this intermediate PNG workflow is 2 step, depending on your requirements for transparency, you may not need to create an action to remove the background (unknown).

 

Also, it would help to provide before-and-after images demonstrating sizing and positioning for both white and grey backgrounds.

Participant
November 5, 2024

Thanks @Stephen Marsh! appreciate you taking the time to provide some options for me 😃

I ended up downloading the trail and then recording an action with the script + a batch for the webp file and i got there in essentially one hit.
Legend

Stephen Marsh
Community Expert
Community Expert
November 6, 2024

@Gnomey1Kenobi 

 

Great, please mark the appropriate reply/replies as a correct answer, thanks.

Stephen Marsh
Community Expert
Community Expert
November 4, 2024

@Gnomey1Kenobi 

 

An alternative solution is to use my WebP Batch Processor script:

 

 

 
The script can run an action before saving the file to WebP, so all you need to do is create the action to remove the background to create transparency and Trim or Fit Image or Image Size and or Canvas Size to 1000 x 1000 px square and you're done!
 
P.S. The script does offer a "Fit Image" option, however, I am unsure of your resizing/trim/position requirements.