-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize_each_image.js
61 lines (50 loc) · 2.6 KB
/
resize_each_image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// use case: resize all open images your current Photoshop session.
// this checks if images are portrait or landscape and gets the dimensions right for each
// Define the widths to which you want to scale your images
var widths = [300, 250, 180];
var heights = [200, 160, 150];
// Define the folder where the output files will be saved
// var outputFolder = Folder.desktop; // This gets the user's desktop folder
var outputFolder = "~/Desktop/sizedimages/";
// Check if there are open documents
if (app.documents.length === 0) {
alert("No open documents to process.");
} else {
// Loop through all open documents
// for (var i = 0; i < 2; i++) {
for (var i = 0; i < app.documents.length; i++) {
var doc = app.documents[i]; // Current document
// Bring the document to the front
app.activeDocument = doc;
// Check if the active layer is locked
if (doc.activeLayer.allLocked) {
alert("The active layer of the document " + doc.name + " is locked. Please unlock and try again.");
continue; // Skip to the next document
}
// Loop through all desired widths
for (var j = 0; j < widths.length; j++) {
// Scale image
// on my particular images, cropping to a tighter ratio than about .63 will result in the top and bottom
// of the image dropping off when resizing width and then specifying canvas height.
// for these images, we need do the reverse: set the overall height, and then adjust the canvas width
if ((parseFloat(heights[j]) / parseFloat(widths[j]) ) < 0.63) {
// reverse it
// Scale image
doc.resizeImage(null, UnitValue(heights[j], 'px'), 72, ResampleMethod.BICUBICSHARPER);
doc.resizeCanvas(UnitValue(widths[j], 'px'), UnitValue(heights[j], 'px'), AnchorPosition.MIDDLECENTER);
} else {
// do it like before
// Scale image
doc.resizeImage(UnitValue(widths[j], 'px'), null, 72, ResampleMethod.BICUBICSHARPER);
doc.resizeCanvas(UnitValue(widths[j], 'px'), UnitValue(heights[j], 'px'), AnchorPosition.MIDDLECENTER);
}
// Define the output file
var outputFile = new File(outputFolder + "/" + doc.name.split('.')[0] + '_' + widths[j] + '.png');
// Set PNG save options
var pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.interlaced = false;
// Save image
doc.saveAs(outputFile, pngSaveOptions, true, Extension.LOWERCASE);
}
}
}