Skip to main content
hameedfarah
Known Participant
June 20, 2022
Question

Batch crop to certain ratios

  • June 20, 2022
  • 1 reply
  • 495 views

Hello,

I have over 2000 photos that I want to crop to certain ratio depending on the closest defined ratio.

The ratios I need are 1:1 and 7:5 and 1:3 They can be both portrait and landscape

Examples,
Original dimensions (px) => Cropped dimensions (px)
100x110 => 100x100
159x50 => 150x50
42x120 => 40x120

How can I do that?

 

I have found this code by @SuperMerlin that I think can be adapted to do this. After checking the image dimensions instead of moving it should crop the image.

 

Problem is I have no idea how to modify the script! Any help would be greatly appreciated.

 

*Bonus point 🙂 if the crop can be "Smart" in a way to detect the important object in the image that would be amazing.

#target bridge   
   if( BridgeTalk.appName == "bridge" ) {  
sortPics = new MenuElement("command", "Sort Landscape - Portrait - Square Pics", "at the end of Tools");
}
sortPics.onSelect = function () { 
var sels = app.document.selections;
if(sels.length < 1) return;
var origFolder = decodeURI(app.document.thumbnail.spec);
var portFolder = Folder(origFolder + "/Portrait");
var landFolder = Folder(origFolder + "/Landscape");
var sqrFolder = Folder(origFolder + "/Square");
if(!portFolder.exists) portFolder.create();
if(!landFolder.exists) landFolder.create();
if(!sqrFolder.exists) sqrFolder.create();
app.synchronousMode = true;
for (var s =0;s< sels.length;s++){
var Thumb = new Thumbnail(sels[s]);
var Rotation = Thumb.rotation;
var Width = Thumb.core.quickMetadata.width * 1;
var Height = Thumb.core.quickMetadata.height *1;
if(Width ==0 || Height == 0) continue;
if(Width > Height && Rotation  == 0) Thumb.moveTo(landFolder);
if(Width < Height && Rotation  == 0) Thumb.moveTo(portFolder);
if(Width > Height && Rotation  != 0) Thumb.moveTo(portFolder);
if(Width < Height && Rotation  != 0) Thumb.moveTo(landFolder);
if(Width == Height) Thumb.moveTo(sqrFolder);
    }
app.synchronousMode = false;
};

*Bonus point 🙂 I am not sure if IM can do this, but if it can do "Smart" cropping and detect the important part of the image that would be great

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
June 20, 2022

I think that Bridge would either need to call Photoshop, or that the script would be better as a purely Photoshop script.

 

Working out if an image is portrait/landscape/square is easy, however, as you require a "tolerance" fuzzy factor in there some math will be required. Additionally, you would also like to have some "intelligence" as well. What is the subject matter of the images? Is there a "single" clearly defined object/subject of prominence?

 

I'll try to dig up some Photoshop scripts for reference...

 

Edit: Here is an example of content-aware scale in a script:

 

hameedfarah
Known Participant
June 20, 2022

Thank you for your reply,

You are correct, there needs to be some calculations done to find the closest ratio.

The images are varied, most are abstract artoworks and don't have a clearly defined subject so in this case a crop anywhere is good enough, but there are a few which do have a subject so a crop around it is best.

Again this "smart" part is not very important, the first cropping step is more important.

 

Thanks again

 

Stephen Marsh
Community Expert
Community Expert
June 20, 2022

OK, how would you do this manually?

 

How would you tell a computer to crop to 100x110 to 100x100? What if the size was 120, 150 or 190?

 

What are the rules?

 

I'm not great at math, however, I may be able to translate clear instructions/rules...

 

I haven't setup tests yet, but my go-to would be to see if there is a basic JavaScript math object to do the job:

https://www.w3schools.com/js/js_math.asp