What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Canvas square


johan1

Member
Messages
16
Likes
1
I have a lot of images (around 850) that are not square, but I need them to be. Is there a way to make the canvas of each of them square, without stretching the images?

I found this script, but I found some images stretched when I used it. It would be absolutely great if someone knows how to change it so that it fits to my whishes!

//// resize images & export PNGs
// by pattedours
//


#target photoshop
app.bringToFront();


// Save current dialog preferences
var startDisplayDialogs = app.displayDialogs;


// Save current unit preferences
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;


var inputFolder = Folder.selectDialog("Select the input folder");
var outputFolder = Folder.selectDialog("Select the output folder");


ProcessImages();


function ProcessImages() {
var filesOpened = 0;
if ( inputFolder == null || outputFolder == null) {


if ( inputFolder == null) {


alert("No source folder selected");
}


if ( outputFolder == null) {


alert("No output folder selected");
}
}else{


var fileList = inputFolder.getFiles();
for ( var i = 0; i < fileList.length; i++ ) {
if ( fileList instanceof File && ! fileList.hidden) {
open( fileList );
ResizeImage();
filesOpened++;
}
}
}
return filesOpened;


}


function ExportPng(filePrefix, fileSuffix){
try
{
var docRef = app.activeDocument;
var docName = app.activeDocument.name.slice(0,-4);
var pngSaveOptions = new PNGSaveOptions();
pngFile = new File( outputFolder + "//" + filePrefix + docName + fileSuffix );
pngSaveOptions.interlaced = false;
//
docRef.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE);


}


catch (e)
{
alert("Error encountered when attempting to save the image. \r\r" + e);
return;
}
};


function ResizeImage()
{


function fitImage() {

var docRef = app.activeDocument;
var docWidth = docRef.width.as("px");
var docHeight = docRef.height.as("px");


if (docWidth < docHeight)
{
docRef.resizeImage(docHeight, docHeight, 72, ResampleMethod.BICUBIC );
}
else if (docWidth > docHeight)
{
docRef.resizeImage(docWidth, docWidth, 72, ResampleMethod.BICUBIC );
}


else if (docWidth == docHeight)
{


}
};


var docRef = app.activeDocument;


var savedState = docRef.activeHistoryState;


fitImage();
app.displayDialogs = DialogModes.NO;
ExportPng("", ".png");


docRef.activeHistoryState = savedState;
docRef.close(SaveOptions.DONOTSAVECHANGES);
docRef = null;
}


// Reset app preferences
app.displayDialogs = startDisplayDialogs;
preferences.rulerUnits = originalRulerUnits;


alert("Operation Complete!" + "\n" + "Images were successfully exported to:" + "\n" + "\n" + outputFolder.toString().match(/([^\.]+)/)[1] + "/");
 
If you upload some images instead of code it would be easier to help you. If I understand you, use the transform tool, you can hold down the ctl/cmd key and move first one handle then the other, and this should straighten them out without warping or stretching them. It isn't the canvas that is not square but the image inside the canvas. Let us know if this helps or not. Again, give us at least one or two image samples so we can see (visual artists here) what you mean.
 
I think the op needs the crop tool. There may be an action or a script to make something square but will only work if all/most of the pictures were not fussy where the crop occurred eg repeating textures or something plain . Otherwise me thinks this will have to be done one at a time.
 
Last edited:
Thanks for your replies. Hopefully this images clears it up for you:

Untitled_1.png

Left: The images how I got them, almost cropped to the edges of the image/product. The file has to be a square in order to be displayed properly on the website where I have to place them.

Middle: What happens when I use the script. It document gets square, as it should, but too bad the image 'squares' or stretches with it.

Right: How I would like it to be.
 
Forget the script. Use an image processing language designed specifically to do these sorts of operations: ImageMagick:
http://www.imagemagick.org/Usage/

ImageMagick can easily resize the canvas or place an image on a larger canvas:

http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=19759
http://www.imagemagick.org/Usage/resize/
http://stackoverflow.com/questions/1787356/use-imagemagick-to-place-an-image-inside-a-larger-canvas

In ImageMagick, you can probably do exactly what you want with one function call (ie, one line of code) instead of a few dozen lines of script.

Tom M
 
I must say I find it pretty hard to actually understand how ImageMagick works. There are so much files...
 
Last edited:
there are 2 lines with " resize image" that need to be changed into "resize canvas"


if (docWidth < docHeight)
{
docRef.resizeCanvas(docHeight, docHeight, AnchorPosition.MIDDLECENTER);
}
else if (docWidth > docHeight)
{
docRef.resizeCanvas(docWidth, docWidth, AnchorPosition.MIDDLECENTER);
}
 
And that worked absolutely great! Thank you very much. Else it would be tons of work.

:thumbsup:

@ Al the other repliers: Thanks all for thinking with me to find a solution.
 

Back
Top