Skip to content

Instantly share code, notes, and snippets.

@mrWeiss0
Created March 14, 2018 17:07
Show Gist options
  • Save mrWeiss0/0e6e998b65e0a2f9739c58162fecd6aa to your computer and use it in GitHub Desktop.
Save mrWeiss0/0e6e998b65e0a2f9739c58162fecd6aa to your computer and use it in GitHub Desktop.
Photoshop javascript to overlay a progressive counter on a group of pictures
/*
* Counter Overlay Generatore
*
* Call the script once to prepare the template,
* adjust font, color and size, and write in
* the text layer a starting number,
* then call the script again to start the process.
* For transparent PNG select Cancel in the file select dialog.
*
*/
if (documents.length && activeDocument.name = "__CNTGEN__"){
counter(activeDocument);
}
else {
createDoc(prompt("Image size (ex. 1920x1080)","1920x1080").split('x'))
}
/*
* Creates a new document of the given size
* with a text layer and a black background
* only used to make the text visible
*/
function createDoc(size, fontSize, fontFamily, fontColor){
var doc = documents.add(
new UnitValue(size[0], "px"), new UnitValue(size[1], "px"), 72,
"__CNTGEN__",
NewDocumentMode.RGB, DocumentFill.TRANSPARENT
);
doc.selection.selectAll();
var black;
(black = new SolidColor()).rgb.hexValue = "000000";
doc.selection.fill(black);
doc.selection.deselect();
doc.activeLayer.name = "__BG__";
doc.artLayers.add()
doc.activeLayer.kind = LayerKind.TEXT;
doc.activeLayer.name = "__CNT__";
var txt = doc.activeLayer.textItem
txt.size = fontSize || "100pt";
txt.font = fontFamily || "ArialMT";
txt.color.rgb.hexValue = fontColor || "ffffff";
txt.position = [doc.width * .85, doc.height * .95];
txt.justification = Justification.RIGHT;
txt.contents = 0;
return doc;
}
/*
* Loops over selected pictures increasing
* counter from the current value of text layer
* and overlayng it over each picture
* If no pictures are selected the script will
* ask for qty and generate transparent PNGs
*/
function counter(doc){
var dest = Folder.selectDialog("Destination folder");
if (!dest) return;
var pics = File.openDialog("Select images, cancel for transparent PNG", true, true);
var qty = pics ? pics.length : Number(prompt("How many?", "10"));
var padding = qty.toString().length;
var txt = doc.artLayers.getByName("__CNT__").textItem;
var i0 = Number(txt.contents) || 0;
doc.artLayers.getByName("__BG__").visible = false;
for(var i = 0; i < qty; txt.contents = i0 + ++i){
if (pics)
var pic = overlayPic(txt.parent, pics[i]);
saveCur(doc, dest.fullName, zeroPad(i0 + i, padding));
if (pics)
pic.remove();
}
activeDocument.artLayers.getByName("__BG__").visible = true;
alert("Done!");
}
function overlayPic(layer, picture){
var pic = open(picture)
var newl = pic.activeLayer.duplicate(layer, ElementPlacement.PLACEAFTER);
pic.close();
return newl;
}
function saveCur(document, path, name){
document.saveAs(new File(path + "\\" + name + ".png"), new PNGSaveOptions(), true);
}
function zeroPad(n, length){
n += "";
while (n.length < length)
n = "0" + n;
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment