Skip to content

Instantly share code, notes, and snippets.

@u840903
Last active February 18, 2020 00:09
Show Gist options
  • Save u840903/b69b3157f9e64cf9802d7b245c4aac6e to your computer and use it in GitHub Desktop.
Save u840903/b69b3157f9e64cf9802d7b245c4aac6e to your computer and use it in GitHub Desktop.
function cropToSelection(top, left, bottom, right) {
var idCrop = charIDToTypeID("Crop");
var desc11 = new ActionDescriptor();
var idT = charIDToTypeID("T ");
var desc12 = new ActionDescriptor();
var idTop = charIDToTypeID("Top ");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idTop, idPxl, top);
var idLeft = charIDToTypeID("Left");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idLeft, idPxl, left);
var idBtom = charIDToTypeID("Btom");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idBtom, idPxl, bottom);
var idRght = charIDToTypeID("Rght");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idRght, idPxl, right);
var idRctn = charIDToTypeID("Rctn");
desc11.putObject(idT, idRctn, desc12);
var idAngl = charIDToTypeID("Angl");
var idAng = charIDToTypeID("#Ang");
desc11.putUnitDouble(idAngl, idAng, 0.000000);
var idDlt = charIDToTypeID("Dlt ");
desc11.putBoolean(idDlt, false);
var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
var idtargetSize = stringIDToTypeID("targetSize");
desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
executeAction(idCrop, desc11, DialogModes.NO);
}
function main() {
// Settings.
const TARGET_RATIO = 1;
// Check if we have open documents.
if (!app.documents.length) {
alert('There are no open documents.');
return false;
}
const document = app.activeDocument;
try{
var bounds = document.selection.bounds;
const selectionWidth = bounds[2] - bounds[0];
const selectionHeight = bounds[3] - bounds[1];
const maxDimension = Math.max(selectionHeight, selectionWidth);
const expandValue = Math.round(maxDimension/20);
document.selection.expand(expandValue);
bound = document.selection.bounds;
cropToSelection (bound[1], bound[0], bound[3], bound[2])
} catch(e){
alert('No selection.');
return false;
}
if (true) {
// Crop without delting pixels.
// Get the image size in pixels.
var width = new UnitValue(document.width, document.width.type);
var height = new UnitValue(document.height, document.height.type);
width.convert('px');
height.convert('px');
const currentRatio = width / height;
// Start by setting the current dimensions.
var resizedWidth = width;
var resizedHeight = height;
// The source image aspect ratio determines which dimension, if any, needs to be changed.
if (currentRatio < TARGET_RATIO) {
resizedWidth = height * TARGET_RATIO;
} else {
resizedHeight = width / TARGET_RATIO;
}
// Apply the change to the image.
document.resizeCanvas(resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
}
};
main();
var doc = app.activeDocument;
var docName = doc.name;
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/) : docName = [docName, docName];
function save(doc, saveFile, qty) {
var options = new PNGSaveOptions()
options.compression = 0
options.interlaced = false
doc.saveAs(saveFile, options, true);
}
for (var i = 0; i < 4; i++) {
var saveName = new File(decodeURI(doc.path) + '/result/' + docName[1] + '_' + i + '.png');
doc.rotateCanvas(90);
save(app.activeDocument, saveName, 10);
}
function selectSubject(){
var idautoCutout = stringIDToTypeID( "autoCutout" );
var desc41 = new ActionDescriptor();
var idsampleAllLayers = stringIDToTypeID( "sampleAllLayers" );
desc41.putBoolean( idsampleAllLayers, false );
executeAction( idautoCutout, desc41, DialogModes.NO );
}
function cropToSelection(top, left, bottom, right) {
var idCrop = charIDToTypeID("Crop");
var desc11 = new ActionDescriptor();
var idT = charIDToTypeID("T ");
var desc12 = new ActionDescriptor();
var idTop = charIDToTypeID("Top ");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idTop, idPxl, top);
var idLeft = charIDToTypeID("Left");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idLeft, idPxl, left);
var idBtom = charIDToTypeID("Btom");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idBtom, idPxl, bottom);
var idRght = charIDToTypeID("Rght");
var idPxl = charIDToTypeID("#Pxl");
desc12.putUnitDouble(idRght, idPxl, right);
var idRctn = charIDToTypeID("Rctn");
desc11.putObject(idT, idRctn, desc12);
var idAngl = charIDToTypeID("Angl");
var idAng = charIDToTypeID("#Ang");
desc11.putUnitDouble(idAngl, idAng, 0.000000);
var idDlt = charIDToTypeID("Dlt ");
desc11.putBoolean(idDlt, false);
var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
var idtargetSize = stringIDToTypeID("targetSize");
desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
executeAction(idCrop, desc11, DialogModes.NO);
}
function main() {
// Settings.
const TARGET_RATIO = 1;
// Check if we have open documents.
if (!app.documents.length) {
alert('There are no open documents.');
return false;
}
const document = app.activeDocument;
selectSubject();
if (true) {
try {
var bounds = document.selection.bounds;
const selectionWidth = bounds[2] - bounds[0];
const selectionHeight = bounds[3] - bounds[1];
const maxDimension = Math.max(selectionHeight, selectionWidth);
const expandValue = Math.round(maxDimension / 20);
document.selection.expand(expandValue);
bound = document.selection.bounds;
cropToSelection(bound[1], bound[0], bound[3], bound[2])
} catch (e) {
alert('No selection.');
return false;
}
}
if (true) {
// Crop without delting pixels.
// Get the image size in pixels.
var width = new UnitValue(document.width, document.width.type);
var height = new UnitValue(document.height, document.height.type);
width.convert('px');
height.convert('px');
const currentRatio = width / height;
// Start by setting the current dimensions.
var resizedWidth = width;
var resizedHeight = height;
// The source image aspect ratio determines which dimension, if any, needs to be changed.
if (currentRatio < TARGET_RATIO) {
resizedWidth = height * TARGET_RATIO;
} else {
resizedHeight = width / TARGET_RATIO;
}
// Apply the change to the image.
document.resizeCanvas(resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
}
const layer = app.activeDocument.artLayers[0];
layer.autoLevels();
layer.autoContrast();
layer.applyAddNoise(7, NoiseDistribution.GAUSSIAN, false);
document.changeMode(ChangeMode.GRAYSCALE);
var docName = document.name;
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/) : docName = [docName, docName];
function save(doc, saveFile, qty) {
var options = new PNGSaveOptions()
options.compression = 0;
options.interlaced = false;
document.saveAs(saveFile, options, true);
}
for (var i = 0; i < 4; i++) {
var saveName = new File(decodeURI(document.path) + '/result/' + docName[1] + '_' + i + '.png');
document.rotateCanvas(90);
save(app.activeDocument, saveName, 10);
}
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment