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!
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] + "/");