Skip to main content
greboguru
Participant
March 21, 2019
Answered

Action: Canvas Size as a calculation?

  • March 21, 2019
  • 3 replies
  • 3059 views

I'm building an Action for series of operations I do at work all the time.

We get zillions of images, and I need to automatically make them 3:4 aspect ratio.

Here's what I need:

IF width > height

  • CANVAS SIZE
  • Height: [width x1.3333333]
  • Vertical: bottom
  • Extension Color: background color

IF width < height

  • CANVAS SIZE
  • Width: [height x0.75]
  • Horizontal: center
  • Extension Color: background color

There are other steps in the action I'm building, but I know how to do them. I even know how to do the "If-Then" part, using Insert Conditional in the Actions panel. I just don't know how to do the "Height = Width x 1.333333" and "Width = Height x 0.75" steps.

Any ideas?

Thanks!

Correct answer Stephen Marsh

A scripted solution would be something like this:

 

Re: fit canvas to specific ratio

 

// https://forums.adobe.com/message/9098032#9098032

#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 = 3;
  var arWidth = 4;

  // 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);
}

 

Prepression: Downloading and Installing Adobe Scripts

3 replies

Participant
September 18, 2024

If anyone else is crawling the dead internet and has this same problem — here's a solution I was able to implement with actions instead of scripts. I used this method while processing a bunch of studio shots from random aspect ratios to all squares, but you could adapt it to different aspect ratios I think.

 

  • unlock background layer
  • extend canvas size to something much larger than you need
  • duplicate baground layer
  • rotate duplicate layer 90 degrees (you could then transform it by a % if you're looking for a different aspect ratio than a square)
  • Trim transparent pixels
  • delete duplicate layer
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
March 22, 2019

A scripted solution would be something like this:

 

Re: fit canvas to specific ratio

 

// https://forums.adobe.com/message/9098032#9098032

#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 = 3;
  var arWidth = 4;

  // 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);
}

 

Prepression: Downloading and Installing Adobe Scripts

greboguru
greboguruAuthor
Participant
March 22, 2019

Woah, thank you for this!!

JJMack
Community Expert
Community Expert
March 21, 2019

Photoshop Scripting can use logic like that. Actions only have conditional steps  you can insert into action and there are very few conditions you can test for. While you can test orientation like landscape and square.  And in this case you can change Canvas size a relative percentage of width and height in actions.  If the percentage is less that 100% you will be cropping your Image.  So with  conditional action steps and several actions you may be able to do what you want to do this time.   However if you really want to user logic automating Photoshop you need to learn how to script Photoshop.   The Process would be contains in a single file.  Conditional actions must be in the same Action set  but several actions are required to be recorded before you can insert  conditional steps.  You should look into Scripting Photoshop.

Your 75% Height  would crop  your Image. If you want to get the actual height and width values and use these in a calculation Scripting is required.

JJMack
greboguru
greboguruAuthor
Participant
March 21, 2019

Ah. I thought it might be something like that. Thank you, JJMack​, for your quick and clear answer.

Regrettably, I know nothing at all about how to do Photoshop scripts. But I'll sniff around, maybe I can figure it out.

Cheers!

jane-e
Community Expert
Community Expert
March 21, 2019

Hi Greboguru,

You might ask in the Photoshop Scripting forum: Photoshop Scripting, or we can move this post if you want us to.