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

How to apply “Front Image” dimensions from one image to another using an Action or Script?

New Here ,
Jul 31, 2025 Jul 31, 2025

Hi All,

This is my first post ,

I’m using Photoshop version 24.7,      and I’m not familiar with scripting.

I want to use the “Front Image” option in the Crop Tool to copy the width, height, and resolution from one image, and apply those exact crop dimensions to a different (target) image.

When I try to record this using an Action, Photoshop does not store the Front Image dimensions — it only records the crop, not the size reference.

 Is there any way to copy dimensions from one image using “Front Image” and apply them automatically to another image, either through an Action or a ready-made script?

I’m looking for a solution where I don’t have to manually type the width/height every time.

Since I’m not familiar with scripting, I’d appreciate it if someone could share a script that does this.

Thank you in advance!Adobe post.jpg

TOPICS
Actions and scripting , Windows
375
Translate
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
Adobe
Community Expert ,
Jul 31, 2025 Jul 31, 2025

Yes, scripting can capture and use the doc width and height and resolution, independent of the front image setting.

That being said, this may or may not be applicable to this command via scripting.

 

There are other ways to crop using one image to others.

 

Are all of the images the same pixel dimensions and resolution?

 

How many images are open at once, and do you need to crop them all?

Translate
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
New Here ,
Aug 01, 2025 Aug 01, 2025

Thank you for your response!

To answer your questions:

     The source (front) image has specific dimensions and resolution, and I want to apply those exact crop settings to other images, which may be different in size or resolution.

     Usually, only 2 images are open at a time — the source image (from which I want to copy dimensions), and the target image (on which I want to apply the crop).

    I don't need to crop multiple images at once. Just one at a time is fine.

What I’m really looking for is a way to:

Open the source image.

Get its width, height, and resolution.

Switch to the target image.

Crop it using those same dimensions (centered or from top-left would be okay).

     

        That’s why I tried to use the “Front Image” option in Photoshop’s Crop Tool,
but this step doesn’t get recorded or added properly in an Action.

 

Actually, here’s my use case:

 

I have a high-resolution original image from which I want to remove the background.
Since I’m not getting clean background removal at full resolution directly in Photoshop, I use an online background removal platform.

However, the problem is that those online tools give me a transparent PNG, but it’s not in the original size or resolution — it’s usually downscaled.

So, what I currently do is:

I open the original high-resolution image and the low-resolution transparent image (from the online tool).

I want to apply the exact dimensions of the original image to the transparent one.

Then I copy and paste the transparent subject from the smaller image to the original image.

I use that pasted subject as a selection or mask on the original image to remove the background, but keep the full quality intact.

This way, I get a high-resolution version with the background removed, using the help of the online cutout.

Now I’m trying to automate or simplify this workflow using Actions or Scripts, especially the step where I apply the original image’s dimensions to the cropped version.

 

Thanks again for any suggestions!

Since I’m not familiar with scripting, if there’s a script or Action-based workaround that could help with this flow, I’d greatly appreciate it!

Translate
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 ,
Aug 01, 2025 Aug 01, 2025

Explaining the true use case is always helpful, as it has absolutely no bearing on your original idea. I'd suggest that you use cloud processing (below right), not local device (below left) for background removal in Photoshop and use the latest 2025  version or beta version to take advantage of improvements made to Select Subject.

 

Stephen_A_Marsh_0-1733837005701.jpeg

 

Upscaling another platform's downscaled output to use as a mask on a high resolution original doesn't seem like a great way to work. I'd really make sure that working in Photoshop for the background removal is truly working for you by exploring all the options.

 

Sample images are always helpful if you can provide links to them or upload here, if you really want to explore such a workflow.

Translate
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 ,
Aug 01, 2025 Aug 01, 2025

@tusibabu 

 

Try this script to copy and apply the PNG transparency from the smaller doc to the larger doc. It is assumed that the smaller sized PNG doc has a single floating layer with transparency with no layer mask. It is also assumed that the larger doc has a single layer, flattened or floating.

 

/*
Copy Transparency from Smaller Doc to Larger Doc.jsx
Stephen Marsh
v1.0 - 1st August 2025: Initial release.
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-apply-front-image-dimensions-from-one-image-to-another-using-an-action-or-script/td-p/15438428
*/

#target photoshop

(function () {

    try {
        if (app.documents.length !== 2) {
            alert("Error: Please open exactly 2 documents. Currently " + app.documents.length + " documents are open.");
            return;
        }

        try {
            var doc1 = app.documents[0];
            var doc2 = app.documents[1];

            /*
            alert("Document 1 identified: " + doc1.name + 
                  "\nDimensions: " + doc1.width.as('px') + " x " + doc1.height.as('px') + " px" +
                  "\nResolution: " + doc1.resolution + " ppi");
            alert("Document 2 identified: " + doc2.name + 
                  "\nDimensions: " + doc2.width.as('px') + " x " + doc2.height.as('px') + " px" +
                  "\nResolution: " + doc2.resolution + " ppi");
            */

            var doc1Width = doc1.width.as('px');
            var doc1Height = doc1.height.as('px');
            var doc2Width = doc2.width.as('px');
            var doc2Height = doc2.height.as('px');

            if (doc1Width === doc2Width && doc1Height === doc2Height) {
                alert("Both documents have the same pixel dimensions (" + doc1Width + " x " + doc1Height + " px). No resizing needed.");
                return;
            }

            var smallerDoc;
            var largerDoc;
            var doc1Area = doc1Width * doc1Height;
            var doc2Area = doc2Width * doc2Height;

            if (doc1Area <= doc2Area) {
                smallerDoc = doc1;
                largerDoc = doc2;
            } else {
                smallerDoc = doc2;
                largerDoc = doc1;
            }

            /*
            alert("Smaller document identified: " + smallerDoc.name +
                  "\nWill be resized to match: " + largerDoc.name);
            */

            app.activeDocument = smallerDoc;

            imageSize(largerDoc.width.as('px'), largerDoc.height.as('px'), largerDoc.resolution);

            app.activeDocument = largerDoc;

            loadSelection(smallerDoc);

            addLayerMask();

            alert('Done!');

            smallerDoc.close(SaveOptions.DONOTSAVECHANGES);

        } catch (error) {
            alert("Error occurred: " + error.message);
        }

    } catch (e) {
        alert("Error:\n" + e.message + "\nLine: " + e.line);
    }

})();


///// Functions /////

function addLayerMask() {
    try {
        var idMk = charIDToTypeID("Mk  ");
        var desc467 = new ActionDescriptor();
        var idNw = charIDToTypeID("Nw  ");
        var idChnl = charIDToTypeID("Chnl");
        desc467.putClass(idNw, idChnl);
        var idAt = charIDToTypeID("At  ");
        var ref76 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idMsk = charIDToTypeID("Msk ");
        ref76.putEnumerated(idChnl, idChnl, idMsk);
        desc467.putReference(idAt, ref76);
        var idUsng = charIDToTypeID("Usng");
        var idUsrM = charIDToTypeID("UsrM");
        var idRvlS = charIDToTypeID("RvlS");
        desc467.putEnumerated(idUsng, idUsrM, idRvlS);
        executeAction(idMk, desc467, DialogModes.NO);
    } catch (e) {
        alert("Error:\n" + e.message + "\nLine: " + e.line);
    }
}

function loadSelection(smallerDoc) {
    try {
        var idsetd = charIDToTypeID("setd");
        var desc465 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref74 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idfsel = charIDToTypeID("fsel");
        ref74.putProperty(idChnl, idfsel);
        desc465.putReference(idnull, ref74);
        var idT = charIDToTypeID("T   ");
        var ref75 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idTrsp = charIDToTypeID("Trsp");
        ref75.putEnumerated(idChnl, idChnl, idTrsp);
        var idLyr = charIDToTypeID("Lyr ");
        ref75.putName(idLyr, smallerDoc.layers[0].name);
        var idDcmn = charIDToTypeID("Dcmn");
        ref75.putName(idDcmn, smallerDoc.name);
        desc465.putReference(idT, ref75);
        executeAction(idsetd, desc465, DialogModes.NO);
    } catch (e) {
        alert("Error:\n" + e.message + "\nLine: " + e.line);
    }
}

function imageSize(width, height, resolution) {
    try {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        descriptor.putUnitDouble(s2t("width"), s2t("pixelsUnit"), width);
        descriptor.putUnitDouble(s2t("height"), s2t("pixelsUnit"), height);
        descriptor.putUnitDouble(s2t("resolution"), s2t("densityUnit"), resolution);
        descriptor.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("bicubicSmoother"));
        executeAction(s2t("imageSize"), descriptor, DialogModes.NO);
    } catch (e) {
        alert("Error:\n" + e.message + "\nLine: " + e.line);
    }
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below):

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Translate
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
New Here ,
Aug 03, 2025 Aug 03, 2025

@Stephen Marsh 
Thank u for the reply,
I am trying your code and will post here what happened, as soon as possible, as I am busy with closing on this project manually

Translate
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 ,
Aug 10, 2025 Aug 10, 2025
quote

@Stephen Marsh 
Thank u for the reply,
I am trying your code and will post here what happened, as soon as possible, as I am busy with closing on this project manually


By @tusibabu

 

How did you go with the script? The whole idea of scripting this was to save you doing this manually...

Translate
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
New Here ,
Aug 20, 2025 Aug 20, 2025

Stephen Marsh,
I just tried your code today on the first screenshot I provided, and the second screenshot shows the result. I think the script worked, but it didn’t place the mask exactly on the subject. The mask was too big and didn’t properly cover my subject.

I feel that it may need some updates, but for now, we can pause here. I will work on this project again when necessary. I sincerely appreciate your effort in creating this script for me—thank you once again.

Translate
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 ,
Aug 20, 2025 Aug 20, 2025
LATEST

@tusibabu 

 

Thanks for your feedback, it's tough flying blind, it works fine with my test images, but your images may not be consistent like mine. That's why I originally wrote:

 

"Sample images are always helpful if you can provide links to them or upload here, if you really want to explore such a workflow."

 

Happy to review when you can supply examples.

Translate
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