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

Community Beginner ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

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

TOPICS
macOS

Views

63

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

Adobe Community Professional , Feb 17, 2022 Feb 17, 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 s...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

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 Beginner ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

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 Beginner ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

NVM my previous reply.  I got it! Paired it with this solution for iterating through layers, unless you have a difference solution that I'm not seeing? 

 

Thanks!!!

3dots

Likes

Translate

Translate

Report

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 Beginner ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

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

 

  1. I used @Stephen_A_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-...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 🙂

 

Likes

Translate

Translate

Report

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 Professional ,
Feb 17, 2022 Feb 17, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

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