Skip to main content
3_dots
Participant
February 17, 2022
Answered

ISO: Script or actions to fill canvas with an image while maintaining aspect ratio

  • February 17, 2022
  • 3 replies
  • 3396 views

I'm on PS CC on MacOX.

 

Hello all, I am trying to find some batching (either actions or script) that will allow a images to be resized to a canvas, fill the canvas and maintain aspect ratio.

 

My images are all various sizes, but are close in size to the canvas.  Some are more narrow or shorter than the canvas and I'd like the image to fill it so there's no white/transparent space. If the image is bigger in all ways, it would be good if the image could shrink to fill the canvas (but not as pressing as the first requirement).  I realize I can cmd-T and drag the images to size manually but I have hundreds to do.  Cropping is perfectly fine and if there's a way to then also batch center the image within the canvas, that would be great. 

 

I will still have to go through and manually inspect each one to make sure I didn't accidentally crop someone's face or something, but automating the initial process will still save me a ton of time.

 

I've been messing with this issue for a couple of days so if I'm not explaining it in a way that a sane person can understand, please ask questions so I can clarify.

 

Thanks! 3_dots

Correct answer Stephen Marsh

Try this script:

 

// https://forums.adobe.com/thread/1968642
// https://forums.adobe.com/message/8022190#8022190
#target photoshop    
var oldPref = app.preferences.rulerUnits    
app.preferences.rulerUnits = Units.PIXELS;    
var doc = activeDocument;   
var iLayer = doc.activeLayer;    
doc.activeLayer = iLayer;    
var scale = Math.max(doc.width/(iLayer.bounds[2]-iLayer.bounds[0]),doc.height/(iLayer.bounds[3]-iLayer.bounds[1])); // Optionally change Math.max to Math.min to fit canvas short side
iLayer.resize (scale*100,scale*100);    
iLayer.translate(doc.width/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,doc.height/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
app.preferences.rulerUnits = oldPref;

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

 

3 replies

Stephen Marsh
Community Expert
Community Expert
October 31, 2024

Another scripted option here:

 

 

* Maintain Aspect Ratio
* Fill Canvas (may crop)
* Fit to Canvas (may distort)

 

3_dots
3_dotsAuthor
Participant
February 18, 2022

If anyone else comes across this solution, here's what I did:

 

  1. I used @Stephen Marsh solution (code below) which I copy/pasted/saved to my local script folder. 
  2. I paired it with this solution https://community.adobe.com/t5/photoshop-ecosystem-discussions/applying-an-action-to-all-the-layers-in-a-document/m-p/12463566#M591104 and copy/pasted saved to my local script folder
  3. Open a file with the final canvas size I want
  4. Load in all my images to individual smart object layers
  5. Create an action that holds the script solution (script from step #1) 
  6.  Select that action in the Actions Panel (all you have to do is click on the name of the action)
  7.  Then load the script from step #2 (File --> Scripts --> Browse --> find the folder you saved your script in step #2)
  8. Wallah! The script from step #2 will run script #1 on all layers.  It's magic 🙂

 

Stephen Marsh
Community Expert
Community Expert
February 18, 2022

Glad this helped, I didn't understand that you were trying to process multi-layer images.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
February 18, 2022

Try this script:

 

// https://forums.adobe.com/thread/1968642
// https://forums.adobe.com/message/8022190#8022190
#target photoshop    
var oldPref = app.preferences.rulerUnits    
app.preferences.rulerUnits = Units.PIXELS;    
var doc = activeDocument;   
var iLayer = doc.activeLayer;    
doc.activeLayer = iLayer;    
var scale = Math.max(doc.width/(iLayer.bounds[2]-iLayer.bounds[0]),doc.height/(iLayer.bounds[3]-iLayer.bounds[1])); // Optionally change Math.max to Math.min to fit canvas short side
iLayer.resize (scale*100,scale*100);    
iLayer.translate(doc.width/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,doc.height/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
app.preferences.rulerUnits = oldPref;

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

 

3_dots
3_dotsAuthor
Participant
February 18, 2022

Awesome, will give it a try now. Anything I need to know? Will it parse through all the image layers automatically?  Do they need to be smart objects or anything else?  Thanks!