Search code examples
imageimage-processingimage-manipulationphotoshopphotoshop-script

Photoshop Action to fill image to make a certain ratio


I am looking to make a photoshop action (maybe this isn't possible, any other application recommendations would be helpful as well). I want to take a collection of photos and make them a certain aspect ration, ex: 4:3.

So I have an image that is 150px wide by 200px high. What I would like to happen is the image's canvas is made to be 267px wide, with the new area filled with a certain color.

So there are two possibilities I can think of:

1) Photoshop actions could do this, but I would have to pull current height, multiply by 1.333333 and then put that value in the width box of the canvas resize. Is it possible to have calculated values in Photoshop actions?

2) Some other application has this feature built in.

Any help is greatly appreciated.


Solution

  • Wow, I see now (after writing the answer) that this was asked a long time ago. . . oh well. This script does the trick.

    This Photoshop script will resize any image's canvas so that it has a 4:5 aspect ratio. You can change the aspect ratio applied by changing arWidth and arHeight. The fill color will be set to the current background color. You could create an action to open a file, apply this script, then close the file to do a batch process.

    Shutdown Photoshop.
    Copy this javascript into a new file named "Resize Canvas.jsx" in Photoshop's Presets\Scripts folder.
    Start Photoshop and in the File - Scripts menu it should appear.

    #target photoshop
    
    main ();
    
    function main ()
    {
        if (app.documents.length < 1)
        {
            alert ("No document open to resize.");
            return;
        }
    
        // These can be changed to create images with different aspect ratios.
        var arHeight = 4;
        var arWidth = 5;
    
        // Apply the resize to Photoshop's active (selected) document.
        var doc = app.activeDocument;
    
        // Get the image size in pixels.
        var pixelWidth = new UnitValue (doc.width, doc.width.type);
        var pixelHeight = new UnitValue (doc.height, doc.height.type);
        pixelWidth.convert ('px');
        pixelHeight.convert ('px');
    
        // Determine the target aspect ratio and the current aspect ratio of the image.
        var targetAr = arWidth / arHeight;
        var sourceAr = pixelWidth / pixelHeight;
    
        // Start by setting the current dimensions.
        var resizedWidth = pixelWidth;
        var resizedHeight = pixelHeight;
    
        // The source image aspect ratio determines which dimension, if any, needs to be changed.
        if (sourceAr < targetAr)
            resizedWidth = (arWidth * pixelHeight) / arHeight;
        else
            resizedHeight = (arHeight * pixelWidth) / arWidth;
    
        // Apply the change to the image.
        doc.resizeCanvas (resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
    }