How to detect transparancy in an image

No clue whenever this is even possible, I couldn’t find anything myself.

In my use-case, I don’t wanna use “select subject” on images that have already been processed.
And sometimes, luckily not very often, my script gets images that already have been processed, so the background is transparent.

So those images have to be ignored/excluded from the subject selection.

Any suggestions on how? Maybe BatchPlay has something?

Like that, you select all transparent pixels, and if the selection is bigger than a certain size, you know that the image is partially transparent?

One way could be to convert transparent pixels into mask and then analyze a mask.

const {executeAsModal} = require("photoshop").core;
const {batchPlay} = require("photoshop").action;

async function actionCommands() {
   const result = await batchPlay(
      [
         {
            _obj: "make",
            new: {
               _class: "channel"
            },
            at: {
               _ref: "channel",
               _enum: "channel",
               _value: "mask"
            },
            using: {
               _enum: "userMaskEnabled",
               _value: "transparency"
            },
            _options: {
               dialogOptions: "display"
            }
         }
      ],
      {
         modalBehavior: "execute"
      }
   );
}

async function runModalFunction() {
   await executeAsModal(actionCommands, {"commandName": "Action Commands"});
}

await runModalFunction();

Hey Jarda,

Sorry for my late reply, only got back to it now.
The issue with this approach is that it errors when no transparency is found, so the user would have to press “ok” to remove the error.

Suggestions on how to disable the error? Or a alternative approach?

Hiya,
try this one:

const { batchPlay } = require("photoshop").action;
const hasTransparentPixels = async () => {
    return await executeAsModal(
        async () => {
            let r = await batchPlay(
                [
                    {
                        _obj: "set",
                        _target: [
                            {
                                _ref: "channel",
                                _property: "selection",
                            },
                        ],
                        to: {
                            _ref: "channel",
                            _enum: "channel",
                            _value: "transparencyEnum",
                        },
                        invert: true,
                    },
                    {
                        _obj: "get",
                        _target: [
                            {
                                _property: "selection",
                            },
                            {
                                _ref: "document",
                                _enum: "ordinal",
                                _value: "targetEnum",
                            },
                        ],
                    },
                    {
                        _obj: "set",
                        _target: [
                            {
                                _ref: "channel",
                                _property: "selection",
                            },
                        ],
                        to: {
                            _enum: "ordinal",
                            _value: "none",
                        },
                    },
                ],
                {}
            );
            return r[1]?.selection ? true : false;
        },
        { commandName: "baa" }
    );
};

console.log(await hasTransparentPixels()); // logs true or false

HTH,
—Davide