Create 100s of plain color png files

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

belinha
Posts: 4
Joined: Sat Nov 20, 2021 8:57 pm

Create 100s of plain color png files

Post by belinha »

Hello,

First post.

I found the below code from this post
viewtopic.php?f=66&t=40450&p=169300&hil ... or#p169300

That almost does what I am looking for.
I need to auto-create +300 solid color images from a series of pre-selected hex codes.
But I need to name each file created with its corresponding color hex value.

So, instead of these images created:
Solid Color 0 5x5.tif
Solid Color 1 5x5.tif
Solid Color 2 5x5.tif
Solid Color 3 5x5.tif

I need these images created with their corresponding color hex value:
dcb149.tif
e8d279.tif
bd6124.tif
c53c12.tif

I tried to modify the code but cannot find where to do it.
My knowledge of scripting is none.

Second minor request: Instead of .tif files I would need .png files.

Thank you in advance for your kind help.

ddcg wrote: Tue Dec 18, 2018 9:09 pm A little late to the party here...stumbled across your question and thought I'd share something I created recently that will do a similar thing.
This script will create a solid color layer with a random color from a series of pre-selected hex codes...so the idea is you populate the array with hex vals for colors you want it to choose from and then it'll randomly pick one of those colors for each iteration of the loop.
The loop is determined by 'n'. Change the value of n to however many files you want it to create and that's it; hit play and walk away :)

Code: Select all

var currentRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.INCHES;

// SAVE PATH
var rootFolder = new Folder(Folder.desktop + "/Solid Colors");

// CREATE SAVE PATH IF IT DOESN'T EXIST
if (!rootFolder.exists) {
    rootFolder.create()
}

// RANDOM NUMBER GENERATOR
function rando(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

// SAVE AS A TIFF
function saveTiff(savePath, newName) {
    
    var activeDoc = app.activeDocument;
    var w = activeDoc.width.value
    var h = activeDoc.height.value
    var revWidth = (Math.floor(w)).toString();
    var revHeight = (Math.floor(h)).toString();

    var saveOpts = new TiffSaveOptions();
         saveOpts.alphaChannels = false;
         saveOpts.interleaveChannels = true;
         saveOpts.imageCompression = TIFFEncoding.TIFFLZW;

    activeDoc.saveAs(new File(savePath + "/" + newName + " " + revWidth + "x" + revHeight + ".tif"), saveOpts);
    activeDoc.close();
}

var n = 10;    // CHANGE N TO THE NUMBER OF FILES YOU WANT THE SCRIPT TO CREATE

var masterWidth = 5;    // SET YOUR DOCUMENT WIDTH
var masterHeight = 5;   // SET YOUR DOCUMENT HEIGHT
var masterRes = 300;    // SET YOUR DOCUMENT RESOLUTION

// ADD MORE HEX CODES TO INCLUDE DIFFERENT COLORS
var hexVals = [
    "dcb149",   //mustard yellow
    "e8d279",   //canary yellow
    "bd6124",   //light orange
    "c53c12",   //orange red
    "991a1a",   //dk red
    "991a38",   //fuscia
    "f1725e",   //salmon
    "441848",   //plum
    "3c1f35",   //blackberry
    "2d1c43",   //grape
    "5088ac",   //sky blue
    "20beda",   //baby blue
    "426277",   //ocean blue
    "1b2a4a",   //navy blue
    "274685",   //royal blue
    "468684",   //sea foam blue / green
    "3a6c4a",   //kelly green
    "304d2d",   //forest green
    "2e3a1c",   //army green
    "4d541d",   //moss green
    "6f6a6c",   //warm med gray
    "252424",   //warm dk gray
    "b9b6b7"    //light gray

]

for (i = 0; i < n; i++) {
    var h = rando(0, (hexVals.length - 1));
    app.foregroundColor.rgb.hexValue = hexVals[h];

    //alert(app.foregroundColor.rgb.hexValue)
    app.documents.add(masterWidth, masterHeight, masterRes, "Untitled " + i, NewDocumentMode.RGB, DocumentFill.WHITE);
    var doc = app.activeDocument;
    var colorLayer = doc.artLayers.add();
    app.activeLayer = colorLayer;
    doc.selection.selectAll();
    doc.selection.fill(app.foregroundColor);
    colorLayer.move(doc.backgroundLayer, ElementPlacement.PLACEBEFORE);
    saveTiff(rootFolder, "Solid Color " + i)
}

app.preferences.rulerUnits = currentRulers;
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Create 100s of plain color png files

Post by Kukurykus »

At beginning of code put:

Code: Select all

displayDialogs = DialogModes.NO
Then 2 saving / closing lines in saveTiff function change to:

Code: Select all

    activeDoc.saveAs(new File(savePath + "/" + hexVals[i] + '.png'), new PNGSaveOptions);
    activeDoc. close(SaveOptions.DONOTSAVECHANGES);
belinha
Posts: 4
Joined: Sat Nov 20, 2021 8:57 pm

Re: Create 100s of plain color png files

Post by belinha »

@Kukurykus
Thank you very much for your kind help.
With your adjustments the code certainly does what I requested.

But the colors generated are wrong.
The name of the png created is not equal to the displayed color.
I suspect it is because the original code randomly pick any hex value and name the file with any other hex value.

Code: Select all

// RANDOM NUMBER GENERATOR
function rando(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Code: Select all

var n = 10;    // CHANGE N TO THE NUMBER OF FILES YOU WANT THE SCRIPT TO CREATE
Because of this random picking, sometimes one same color is generated 2 or more times with different names.
I hadn't noticed this before, because all the files were named: color 1, 2, 3, etc.

How to remove that random picking option and create the images sequentially with the correct hex value and the correct name?

thank you, in advance.
Last edited by belinha on Sun Nov 21, 2021 3:16 am, edited 1 time in total.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Create 100s of plain color png files

Post by Kukurykus »

Change another 3 lines in the code to:

Code: Select all

for (i = 0; i < hexVals.length; i++) {
    app.foregroundColor.rgb.hexValue = hexVals[i];
belinha
Posts: 4
Joined: Sat Nov 20, 2021 8:57 pm

Re: Create 100s of plain color png files

Post by belinha »

@Kukurykus
Yes. Now, with your corrections the code works as expected.
Thank you, very much.

I removed the below 3 parts of the original code:

Code: Select all

// RANDOM NUMBER GENERATOR
function rando(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Code: Select all

    var revWidth = (Math.floor(w)).toString();
    var revHeight = (Math.floor(h)).toString();

Code: Select all

var n = 10;    // CHANGE N TO THE NUMBER OF FILES YOU WANT THE SCRIPT TO CREATE
So the final code looks like this, and seems to be working ok.
Thank you.

Code: Select all

displayDialogs = DialogModes.NO
var currentRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// SAVE PATH
var rootFolder = new Folder(Folder.desktop + "/Colores");

// CREATE SAVE PATH IF IT DOESN'T EXIST
if (!rootFolder.exists) {
    rootFolder.create()
}

// SAVE AS A TIFF
function saveTiff(savePath, newName) {
    var activeDoc = app.activeDocument;
    var w = activeDoc.width.value
    var h = activeDoc.height.value
    var saveOpts = new TiffSaveOptions();
         saveOpts.alphaChannels = false;
         saveOpts.interleaveChannels = true;
         saveOpts.imageCompression = TIFFEncoding.TIFFLZW;
         activeDoc.saveAs(new File(savePath + "/" + hexVals[i] + '.png'), new PNGSaveOptions);
         activeDoc. close(SaveOptions.DONOTSAVECHANGES);
}

var masterWidth = 500;    // SET YOUR DOCUMENT WIDTH
var masterHeight = 750;   // SET YOUR DOCUMENT HEIGHT
var masterRes = 300;    // SET YOUR DOCUMENT RESOLUTION

// ADD MORE HEX CODES TO INCLUDE DIFFERENT COLORS
var hexVals = [
      "dcb149",   //mustard yellow
      "20beda",   //baby blue
      "304d2d",   //forest green
]

for (i = 0; i < hexVals.length; i++) {
    app.foregroundColor.rgb.hexValue = hexVals[i];

    //alert(app.foregroundColor.rgb.hexValue)

    app.documents.add(masterWidth, masterHeight, masterRes, "Untitled " + i, NewDocumentMode.RGB, DocumentFill.WHITE);
    var doc = app.activeDocument;
    var colorLayer = doc.artLayers.add();
    app.activeLayer = colorLayer;
    doc.selection.selectAll();
    doc.selection.fill(app.foregroundColor);
    colorLayer.move(doc.backgroundLayer, ElementPlacement.PLACEBEFORE);
    saveTiff(rootFolder, + i)
}

app.preferences.rulerUnits = currentRulers;

I do not want to be a pain in the neck. One detail I noticed, is that the png's created are very heavy (+1MB per file) compared to one created manually (5KB per file). Same size, same resolution.
I think the code is still compressing the images as TIFF.

Image

Thank you.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Create 100s of plain color png files

Post by Kukurykus »

Remove saveTiff function while its caller replace by:

Code: Select all

	(png = new PNGSaveOptions).compression = 9
	doc.saveAs(File(rootFolder + '/' + hexVals[i]), png)
	doc.close(SaveOptions.DONOTSAVECHANGES)
belinha
Posts: 4
Joined: Sat Nov 20, 2021 8:57 pm

Re: Create 100s of plain color png files

Post by belinha »

@Kukurykus
Thank you, thank you very much.
This is what I needed.

With your kind help I also learned a little bit how a script works.

The final code:

Code: Select all

displayDialogs = DialogModes.NO
var currentRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// SAVE PATH
var rootFolder = new Folder(Folder.desktop + "/Colores");

// CREATE SAVE PATH IF IT DOESN'T EXIST
if (!rootFolder.exists) {
    rootFolder.create()
}

// SAVE AS A TIFF
function saveTiff(savePath, newName) {
    (png = new PNGSaveOptions).compression = 9
    doc.saveAs(File(rootFolder + '/' + hexVals[i]), png)
    doc.close(SaveOptions.DONOTSAVECHANGES)
}

var masterWidth = 500;    // SET YOUR DOCUMENT WIDTH
var masterHeight = 750;   // SET YOUR DOCUMENT HEIGHT
var masterRes = 300;    // SET YOUR DOCUMENT RESOLUTION

// ADD MORE HEX CODES TO INCLUDE DIFFERENT COLORS
var hexVals = [
      "dcb149",   //mustard yellow
      "20beda",   //baby blue
      "304d2d",   //forest green
]

for (i = 0; i < hexVals.length; i++) {
    app.foregroundColor.rgb.hexValue = hexVals[i];

    //alert(app.foregroundColor.rgb.hexValue)

    app.documents.add(masterWidth, masterHeight, masterRes, "Untitled " + i, NewDocumentMode.RGB, DocumentFill.WHITE);
    var doc = app.activeDocument;
    var colorLayer = doc.artLayers.add();
    app.activeLayer = colorLayer;
    doc.selection.selectAll();
    doc.selection.fill(app.foregroundColor);
    colorLayer.move(doc.backgroundLayer, ElementPlacement.PLACEBEFORE);
    saveTiff(rootFolder, + i)
}

app.preferences.rulerUnits = currentRulers;

Once again, thank you very much.