Copy link to clipboard
Copied
Hi all,
I currently have a large number of png files that are for web use. For our website each image needs to be at a 1:1 ratio and cropped. For the cropping part of the script I just batched all of the images using the trim function. This leaves me with a ton of images with different widths and heights. Is it possible to run a resizing script that will match the larger dimension of each image whether it be the height or width so everything will be at a 1:1 ratio?
Thanks for the help
It should work with pngs. So according to your last post, it looks like you're trimming rather than "cropping." So you don't actually want to crop into the image. Then you want the image to be a 1:1 aspect ratio, so that would mean increasing the canvas size, rather than resizing the image to the longest dimension. Is that right? If so, try this script. All is does is resize the canvas.
...#target photoshop
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;
var lSide = parseInt(Math.m
Copy link to clipboard
Copied
Yes, that can be done, but if you want the 1:1 ratio, you really need to manually crop, or you really don't know where the image will be cropped, unless you don't mind distorted images. Image Processor will resize image to a max size.
Copy link to clipboard
Copied
I am actually resizing the canvas to 1:1 after trimming so its the canvas that I need at a 1:1 processor so the image won't get distorted as you said. Can image processor resize the canvas to a max size as well?
Copy link to clipboard
Copied
Image processor will resize the longest side image to whatever you put in the field to resize it to.
Copy link to clipboard
Copied
I understand that but the 1000s of images I have some have a bigger height and some have a bigger width. I am trying to find a way to have it take whatever is the bigger of the two whether it be the height or the width and match it, and move to the next image without having the manually do each one.
Copy link to clipboard
Copied
No your Action need to make the 1:1 the will be very easy to do if a centered 1:1 crop or a 1:1 crop from the top left corner will be acceptable. You can use my Plug-in script in your action. Aspect Ratio Selection. It can make selections or Paths that are rectangle or ovals that have the aspect ratio you need. It in my crafting actions package with along with other script I wrote for use in Action the Adobe introduce a bug in CC 2015.5 that requires you to add garbage to documents metadata to get around the bug otherwise my run twice scripts will be bitten by Adobe designer Bug.
Crafting Actions Package UPDATED Aug 10, 2014 Added Conditional Action steps to Action Palette Tips.
Contains
When recording the Action the scripts Dialog will look like this. The Dialog will not display when the action is played the script records the settings you used recording the action into the Actions step when the action is played these setting are used the script bypasses displaying its dialog so the action can be batched.
Copy link to clipboard
Copied
Regular crop tool can have specified widht and height... so it will crop and resize your image in one step.
Copy link to clipboard
Copied
Still looking for any suggestions on this. Another quick example of what I am trying to do is I have an image that is 4000Wx3000H and a second one that is 3200Wx3900H. What I am hoping a script can do is trim the pngs, and then resize to 4000WX4000H and 3900WX3900H. After the trim the dimensions will change, but basically I want to match what is bigger, whether is be the height or width.
Thanks
Copy link to clipboard
Copied
two step action
Step 1 Aspect Ratio selection 1 1 center rectangle replace selection
Step 2 Image Crop
Action can be batched....
Copy link to clipboard
Copied
Hey JJ, how do you get to the aspect ratio selection screen? Thanks for the help, kind of a noob at this stuff and trying to figure it out.
Copy link to clipboard
Copied
It is a plug-in script I wrote included in my Crafting Package I had posted information about that package in this thread.
It is a Plug-in Script you can make complex selections and paths with it in actions.
AspectRatioSelection.jsx Makes Aspect Ratio Selections or Paths supports both rectangle and oval selections and the normal selection options (Replace, Add, Subtract, Intersect) Feather, anti-alias and border. Useful utility for cropping and framing actions.
The aspect ratio is also made relative to the document current orientation Portrait or Landscape 1:1 is neither. So if you set 2 and 3 for the ratio values the Selection or Path will be 2:3 for portraits and 3:2 for landscapes
I wrote it to be use in Actions. As a user all you need to do is use it in an action. I did the programming required.
Copy link to clipboard
Copied
Try this:
#target photoshop
var doc = activeDocument;
var lSide = parseInt(Math.max(doc.width,doc.height));
var sSide = parseInt(Math.min(doc.width,doc.height));
var dW = parseInt (doc.width);
var dH = parseInt (doc.height);
cropImage ();
doc.resizeImage(lSide,lSide,undefined,ResampleMethod.BICUBIC);
function cropImage(){
var x1 = (dW-sSide)/2;
var y1 = (dH-sSide)/2;
var x2 = x1+sSide
var y2 = y1+sSide
var idCrop = charIDToTypeID( "Crop" );
var desc6 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var desc7 = new ActionDescriptor();
var idTop = charIDToTypeID( "Top " );
var idPxl = charIDToTypeID( "#Pxl" );
desc7.putUnitDouble( idTop, idPxl, y1 );
var idLeft = charIDToTypeID( "Left" );
var idPxl = charIDToTypeID( "#Pxl" );
desc7.putUnitDouble( idLeft, idPxl, x1 );
var idBtom = charIDToTypeID( "Btom" );
var idPxl = charIDToTypeID( "#Pxl" );
desc7.putUnitDouble( idBtom, idPxl, y2 );
var idRght = charIDToTypeID( "Rght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc7.putUnitDouble( idRght, idPxl, x2 );
var idRctn = charIDToTypeID( "Rctn" );
desc6.putObject( idT, idRctn, desc7 );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc6.putUnitDouble( idAngl, idAng, 0.000000 );
var idDlt = charIDToTypeID( "Dlt " );
desc6.putBoolean( idDlt, false );
var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
var idpureAspectRatio = stringIDToTypeID( "pureAspectRatio" );
desc6.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idpureAspectRatio );
var idCnsP = charIDToTypeID( "CnsP" );
desc6.putBoolean( idCnsP, true );
executeAction( idCrop, desc6, DialogModes.NO );
}
Copy link to clipboard
Copied
Thanks for the help Chuck, this script seemed to freeze my phtoshop about 1/6 of the loading bar in.
Copy link to clipboard
Copied
What version of PS are you using?
Copy link to clipboard
Copied
Adobe photoshop CC 2017.0.1 release
Copy link to clipboard
Copied
His script work on My cc 2017 but it will interpolate images up in size You will loose some image quality. Its not simply a crop. It first upsizes the image
There can be a big difference. Het anu image starte ate 4.3 MB His Crop is 7.5MB mine is 1.5MB
Copy link to clipboard
Copied
Yea, it upsizes the image, as that's what he stated he wanted: crop 1:1 to smaller dimension, then size to the full size of the longer side. Not the best thing to do - I agree.
Script should work on 2017, what's what I'm running it on too.
Copy link to clipboard
Copied
Thanks again for the constant help guys. I will try again. Does it matter that all the images that I am using are pngs possible causing that script to freeze up? I will try and explain again simply what I am trying to do (if that helps at all).
1. I have a bunch of pngs with a transparent background in a folder.
2. My first action is to Select Image > Trim and take the top left pixel colour. This is what we have been doing to crop the images.
3. My next action which is causing this discussion is to match the highest dimension whether it be the height or width.
4. Save the resulting image in a new folder and move on to the next.
Sorry again if I am not giving the best of details. I am fairly new with photoshop and am working on a massive overhaul of images in a catalogue.
Everything is much appreciated.
Copy link to clipboard
Copied
It should work with pngs. So according to your last post, it looks like you're trimming rather than "cropping." So you don't actually want to crop into the image. Then you want the image to be a 1:1 aspect ratio, so that would mean increasing the canvas size, rather than resizing the image to the longest dimension. Is that right? If so, try this script. All is does is resize the canvas.
#target photoshop
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;
var lSide = parseInt(Math.max(doc.width,doc.height));
canSize ();
function canSize (){
var idCnvS = charIDToTypeID( "CnvS" );
var desc11 = new ActionDescriptor();
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc11.putUnitDouble( idWdth, idPxl, lSide );
var idHght = charIDToTypeID( "Hght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc11.putUnitDouble( idHght, idPxl, lSide );
var idHrzn = charIDToTypeID( "Hrzn" );
var idHrzL = charIDToTypeID( "HrzL" );
var idCntr = charIDToTypeID( "Cntr" );
desc11.putEnumerated( idHrzn, idHrzL, idCntr );
var idVrtc = charIDToTypeID( "Vrtc" );
var idVrtL = charIDToTypeID( "VrtL" );
var idCntr = charIDToTypeID( "Cntr" );
desc11.putEnumerated( idVrtc, idVrtL, idCntr );
executeAction( idCnvS, desc11, DialogModes.NO );
}
Copy link to clipboard
Copied
That worked Chuck.
Thanks for all of the help.
Copy link to clipboard
Copied
If the png have a transparency in the background you will do better with trim transparency on all four sides, The top left pixel may has a color which may or may not trim anything. Trim transparency will trim to the image bounds.
Copy link to clipboard
Copied
I will adjust that as well.
Thanks as well for the help JJ.
All the best