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

Scripting: Batch selecting/deleting background

Community Beginner ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

Hello, I'm trying to write a JS script for Photoshop CS unfortunately I'm very new to js and can't find a way to solve it.

I have a batch of image files with a background that I want to delete/ or set white. Sample images below.

I do it manually with a 'quick selection tool'. I select the tool, press left button in the left top corner and move it right-down-left-top which selects the background. Right click and now I create a new layer via cutting which splits the background and the main picture. I delete the background and save the picture in a different folder.

I tried action manager but it doesnt record everything and it cant select the background.

k9296.jpgk10535.jpg

Thank you for your help

TOPICS
Actions and scripting

Views

5.3K

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 Beginner , Jun 21, 2017 Jun 21, 2017

Dear JJMack, thank you for your interest.

Yes, that was a poor choice of the images for the example - Dylan Dog looses image data, an error I accept. I'll correct it manually. Unfortunately I don't know how masks work.

I was ambiguous, I want to delete background and/or change it to pure white/other color or just leave it and save it as transparent. Saving it as transparent should work fine /white background jpg is also ok/

Unfortunately if you are not experience in scripting you won't be able to h

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

morb82035406  wrote

Hello, I'm trying to write a JS script for Photoshop CS unfortunately I'm very new to js and can't find a way to solve it.

I have a batch of image files with a background that I want to delete/ or set white. Sample images below.

I do it manually with a 'quick selection tool'. I select the tool, press left button in the left top corner and move it right-down-left-top which selects the background. Right click and now I create a new layer via cutting which splits the background and the main picture. I delete the background and save the picture in a different folder.

I tried action manager but it doesnt record everything and it cant select the background.

k9296.jpgk10535.jpg

What you wrote above is pure fiction from your imagination.  The example you show  is science fiction.  Two different images with different Aspect ratio size and content. Not a single image example of with and without  a background.

You also can not select the background in either of those images with the manual  method you write you do it with. If that method did work you could record it in an action. You would need to enable tool recording while recording the action.  However, the action would only work on other documents that are the same size and resolution as the document you recorder the action with.  Other size document the tools recording will not work correctly.  

Before you can BS about Photoshop you should learn Photoshop perhaps then you can make your fiction believable.

Your manual method makes these selections

Capture.jpg

JJMack

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 ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

anyone else with scripting experience?

/ I don't know why this guy cropped both pictures befor using 'quick selection tool'. I want to select/delete background not crop the picture/

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 ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

The guy cropped your image because because he thought you wanted to remove the background from your images.  If what I cropped is what you want to remove you will not be able to automate that unless you make that background more uniform.  The one you posted will not select with the Magic wand or the Quick selection brush stroked across the top or side Stroking all four side sides and recording the strokes with tool recording in a action will work in some cases any object the has a border area that is close to the back ground and all the document need to be the same size for the stroke to be on the four sides.  Here is a screen capture of you two image with the Quick selection tool stroke on the left side. If your background was more uniform the Magic wand  with a low tolerance in contiguous mode sampling 8 point on the edges may work better than the Quick selection tool.  Video uses green screen backgrounds to make removing a single color easier and skin tones doe not have green.  Your Backgrounds are not one color and your colors can be in the borders of your objects.....   You can not select the background with the method you state you use.  Your method may work sometimes.

Capture.jpg

Here I created a path on all four sides and stroke it with the quick selection brush.  You can see your method only works on one of your image, not perfectly there are border issues and with your first image part of the object was selected and would be removed..

Capture.jpg

I have a little some scripting knowledge and a lot of experience removing backgrounds.....

You need to understand how Photoshop works better.

I also have a lot of knowledge recording actions so I recorded an action that does what you say you do.  You can download it and batch it you will find it does not works.  I also could not record it with my CC 2017 for If I try to stroke a path with CC 2017 on windows 10 preview CC 2017 crashes so I use CC 2014.  You could try the action in CC 2017. Your CC 2017 may not crash.  I put a white below your second image so you can see the border issues better. Testset.atn

Capture.jpg

The action I posted does not play in CS2 for CS2 does not have the Quick Selection tool.  The Action Plays correctly in CS6, CC, CC 2014, and CC 2015 and works the way I showed not the way you think it will.  It does not Play in CC 2015.5 and CC 2017 because of Bugs in those versions of Photoshop.  CC 2015.5 anf CC 2017 fail to stroke the path with the quick selection tool because of a program error.  CC 2017 also will crash if I try to stroke a path.  The are quit a few bugs in Photoshop and Adobe keeps adding new ones.

I use CC 2014...............................

JJMack

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 ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

Your Way Layer Via Cut Delete layer You will loose image data,masking will not.

Capture.jpg

JJMack

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 ,
Jun 21, 2017 Jun 21, 2017

Copy link to clipboard

Copied

Dear JJMack, thank you for your interest.

Yes, that was a poor choice of the images for the example - Dylan Dog looses image data, an error I accept. I'll correct it manually. Unfortunately I don't know how masks work.

I was ambiguous, I want to delete background and/or change it to pure white/other color or just leave it and save it as transparent. Saving it as transparent should work fine /white background jpg is also ok/

Unfortunately if you are not experience in scripting you won't be able to help me. I need it done not done better, unfortunately.

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 ,
Jun 21, 2017 Jun 21, 2017

Copy link to clipboard

Copied

Scripting it would have the same results as the action. That script may even need to use to use some action manager code.  The Actions I posted can  be converted into a script with xtools or could have been recorded as a script in the first place using Adobe's  Scriptlistened plug-in.  A script does not change what Photoshop can do.

Your examples show clearly show you method can not remove backgrounds.  There is no single method that can remove all backgrounds.  Adobe extract plugin was so bad Adobe removed. If a method was possible Photoshop would have it as a feature.  Photoshop does have tools and features that can help you remove some backgrounds. Some Backgrounds are impossible to remove. You do have a lot to learn.

If a method was possible I could most likely hack it in a Photoshop Script.  I know quits a bit about what a script can do and can not do.  A script can not do what is not possible to do with Photoshop.  If Photoshop can remove and image's background a script to do the same may not be possible for the most powerful tool you have using Photoshop can not be put in a script. The gray matter between your ears can not be transplanted into a script.

The second Action in the action set I posted does your exact method. You method simply does not work not single method is possibly.

JJMack

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 ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

JJMack  wrote

Scripting it would have the same results as the action. That script may even need to use to use some action manager code.  The Actions I posted can  be converted into a script with xtools or could have been recorded as a script in the first place using Adobe's  Scriptlistened plug-in.  A script does not change what Photoshop can do.

Can you record it as a script and show it to me? And I know you are not a good scripter or you are not a scripter at all but maybe that's what you practiced befor. Thank you.

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 ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

You think you know  more then you do.  I never wrote that I am a bad scripted I'm not a bad scripted. I just do not resort to scripting that often for it  is work to write a script and who need work. I don't.  There there is nothing to show for recorfing recording script.  You just install Adobe Plug-in Scriptlistener and everything you do in Photoshop that is record-able is recorded in Javescript and VSB code in log files in Action manager code.  The thing is Action Manager core is not easy to read so it not that easy to extract the script code that you need for you process from the log file and make the extraction a script file that you can add logic to. For like actions, recorded script are step step step no logic you did the logic performing  the steps the way you did.  Your script requires not logic it is step step etc.  you could just use the extracted code.  So you would better off recording the action.  You can convert the action to a script using xTools. It will be action manager code but more readable then what the Scriptlistener plug-in records and there is no extracting code and making a script the script is created for you.  The thing is the Action will work faster. Its steps do not need to be interpreted by a Photoshop ScriptingSupport plugin before a step can be performed by Photoshop.  The action steps require not extra processing to interpret what to do they are just performed as recorded.  Why are you hell bent on a script one is not need to do your process.   The problem is your process does not work.

JJMack

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 ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

LATEST

OK I'm done have it exactly your way just remember I have told you many times your process does not  work.  Here is your process scripted exactly the way you wrote you do it.

#target photoshop

// morb.jsx

//

// Generated Thu Jun 22 2017 09:14:22 GMT-0400

//

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

// morb

function morb() {

  // Set

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(cTID('null'), ref1);

    var desc2 = new ActionDescriptor();

    var desc3 = new ActionDescriptor();

    desc3.putUnitDouble(cTID('Hrzn'), cTID('#Prc'), 0.0);

    desc3.putUnitDouble(cTID('Vrtc'), cTID('#Prc'), 0.0);

    desc2.putObject(cTID('Strt'), cTID('Pnt '), desc3);

    var desc4 = new ActionDescriptor();

    desc4.putUnitDouble(cTID('Hrzn'), cTID('#Prc'), 100.0);

    desc4.putUnitDouble(cTID('Vrtc'), cTID('#Prc'), 0.0);

    desc2.putObject(cTID('End '), cTID('Pnt '), desc4);

    desc2.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 1);

    desc1.putObject(cTID('T   '), cTID('Ln  '), desc2);

    executeAction(cTID('setd'), desc1, dialogMode);

  };

  // Stroke

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(cTID('null'), ref1);

    desc1.putClass(cTID('Usng'), sTID("quickSelectTool"));

    executeAction(cTID('Strk'), desc1, dialogMode);

  };

  // Layer Via Cut

  function step3(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    executeAction(sTID('cutToLayer'), undefined, dialogMode);

  };

  // Delete

  function step4(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));

    desc1.putReference(cTID('null'), ref1);

    executeAction(cTID('Dlt '), desc1, dialogMode);

  };

  // Delete

  function step5(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(cTID('null'), ref1);

    executeAction(cTID('Dlt '), desc1, dialogMode);

  };

  step1();      // Set Path across top

  step2();      // Stroke Path with quick selection tool

  step3();      // Layer Via Cut

  step4();      // Delete Current Layer

  step5();      // Delete Work Path

};

//=========================================

//                    morb.main

//=========================================

morb.main = function () {

  morb();

};

morb.main();

JJMack

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