├── README.md └── remove-bg.jsx /README.md: -------------------------------------------------------------------------------- 1 | # batch-bg-remover-photoshop 2 | 3 | There is a new feature on Adobe Photoshop 2020 CC to select subject of a Image. This simple script uses this tool to remove the background of all images in a specified folder. 4 | 5 | Here is the detailed video on how to use this script: 6 | https://youtu.be/6ICVsi2pWyk 7 | 8 | Plase support us to keep the work up if you think this is usefull. No minimum amout. 9 | PayPal: milansachithra@gmail.com 10 | 11 | # Configuration 12 | 13 | set sourse images folder: line 13 14 | 15 | set output folder: line 15 16 | 17 | set fill or make transparent the background: line 22 18 | 19 | set background fill color: line 18 to 20 (enter RGB values from 0 to 255) 20 | 21 | # New Features 22 | Added support to use a preset image as background. change variable on line 25 to true. Then open the image that you need to keep as background. Then run the script in the same way. 23 | 24 | # Help us to keep the work up 25 | 26 | Donations : PayPal milansachithra@gmail.com 27 | BTC: bitcoin:BC1QV72SP7PS6Z2TCW7AMZ3C4LHC7V7FKGU74VDMK6?label=PS%20Script&message=Thank%20you%20for%20supporting%20us 28 | 29 | YouTube subscribe: https://www.youtube.com/channel/UCfCZ8sBw4YCKnaTyH-FAXGA/featured 30 | 31 | # Contact me 32 | kavithipublications@gmail.com 33 | -------------------------------------------------------------------------------- /remove-bg.jsx: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------------ 2 | This scripts is created by Kavindu Pasan Kavithilaka 3 | on 2020-03-15 4 | updated on 2020-12-13 5 | This scripts supports Photoshop CC 2020 and above versions. 6 | How to use the script: https://youtu.be/6ICVsi2pWyk 7 | --------------------------------------------------------------------------------------*/ 8 | 9 | 10 | /*------------------------------------------------------------------------------------ 11 | Configure following paramers before running the script 12 | --------------------------------------------------------------------------------------*/ 13 | //Place all images needs to be processed in a folder. Add the path below. 14 | var sourceFolder = Folder("C:\\ps\\src"); 15 | //Add the path of an existing folder below to save the output. 16 | var saveFolder = new Folder("C:\\ps\\out"); 17 | //Fill color of the background 18 | var colorRef = new SolidColor; 19 | colorRef.rgb.red = 255; 20 | colorRef.rgb.green = 255; 21 | colorRef.rgb.blue = 255; 22 | //Set blow to true to make the background transparent. 23 | var isTransparent = true; 24 | //Set below to true to use an image as background 25 | var isImageBg = true; 26 | //If isImageBg is set to true, 27 | //it's required to the background image to be preopened in photohsop 28 | //Backdound image must be the active document 29 | //----------------------------------------------------------------------------------- 30 | 31 | 32 | //Check if it's selected to use an image as background 33 | if(isImageBg){ 34 | //Store background image and a variable 35 | var doc_bg = app.activeDocument; 36 | } 37 | 38 | 39 | //Cheks if the source folder is null 40 | if (sourceFolder != null) 41 | { 42 | //The following line will list all files (not only image files) on the source folder. 43 | //If you have any non-image files (even hidden) , please see the next comment. 44 | //var fileList = sourceFolder.getFiles(); 45 | //Comment the above line and uncomment the following line to filter specific file types. 46 | //Try filter files types if the script fails. 47 | var fileList = sourceFolder.getFiles(/\.(jpg|jpeg|png|tif|psd|crw|cr2|nef|dcr|dc2|raw|heic)$/i); 48 | } 49 | else{ 50 | alert("No images found on source folder"); 51 | } 52 | 53 | 54 | //Now this will open every file in the file list 55 | for(var a = 0 ;a < fileList.length; a++){ 56 | //Open file in photoshop 57 | app.open(fileList[a]); 58 | 59 | // Select subject 60 | var idautoCutout = stringIDToTypeID( "autoCutout" ); 61 | var desc01 = new ActionDescriptor(); 62 | var idsampleAllLayers = stringIDToTypeID( "sampleAllLayers" ); 63 | desc01.putBoolean( idsampleAllLayers, false ); 64 | try{ 65 | executeAction( idautoCutout, desc01, DialogModes.NO ); 66 | } 67 | catch(err){} 68 | // Invert the selection 69 | app.activeDocument.selection.invert(); 70 | 71 | 72 | //Now the background is selected. Next step is to fill or clear the selection. 73 | if(isTransparent){ 74 | //Make active layer a normal layer. 75 | activeDocument.activeLayer.isBackgroundLayer = false; 76 | //Make the selection transparent 77 | app.activeDocument.selection.clear(); 78 | } 79 | else{ 80 | app.activeDocument.selection.fill(colorRef); 81 | } 82 | 83 | 84 | //Check if it's selected to use an image as background 85 | if(isImageBg){ 86 | //Store main document to a variable 87 | var main_doc = app.activeDocument; 88 | //Swich to background image 89 | app.activeDocument = doc_bg; 90 | //Copy background to the main image 91 | app.activeDocument.activeLayer.duplicate(main_doc, ElementPlacement.PLACEATEND); 92 | //Switch to the main image 93 | app.activeDocument = main_doc; 94 | } 95 | 96 | 97 | //Now the image is proccessed. Next step is saving the image. 98 | //Create the file name 99 | var fileName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 100 | pngSaveOptions = new PNGSaveOptions(); 101 | //Edit png options here. 102 | //Save image as PNG 103 | app.activeDocument.saveAs(new File(saveFolder +'/'+ Date.now() + "_" + fileName + '.png'), pngSaveOptions, true, Extension.LOWERCASE); 104 | //Close image whithout saving as PSD 105 | app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 106 | } 107 | --------------------------------------------------------------------------------