• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to generate individual swatch jpegs from a color library

Explorer ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

Hi

I have a colour library with more than 200 RGB colours, divided into 28 groups (representing products) of up to 30 colours in each.

I frequently need to generate separate JPEG files 1000x1000px (or sometimes larger) of solid colour, and name the JPEG file with the name of the Group (the product) and the name of the Swatch (the colour) - ie "Group_Swatch.jpg" - for example "Widget100mm_BrightGreen.jpg", where "Widget100mm" is the name of the colour group in my swatch library, and "BrightGreen" is the name of the colour.

So far it has been a simple process of generating these manually, when required - an individual one is quite quick, about 2 mins, including manually renaming. But now I have a new batch of more than 1,000 colours for some 150 different products.

I am sure it is possible to script this process and sit back and watch Photoshop generate these simple, small JPEGS by itself, with unique names. But my scripting skills are not up to the task.

Can anyone point me in the right direction?

John

Photoshop CC 22.4.0, 2018 MacBook Pro, OS 10.15, 32Gb RAM

TOPICS
Actions and scripting

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

Screenshots, sample files and more detailed information on the "colour library" would be helpful.

 

The devil is in the details.

 

P.S. PNG, GIF, TIFF and other lossless compression methods would be better than JPG for a large solid/flat area of colour (both in compression and not having any artifacts or potential colour distortion).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

»The devil is in the details.«

Like the Color Space detail; if the target Color Space is the Working Space one may be able to ignore it but ultimately it might be better to include a clear definition in the Script. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

Thanks for the prompt replies guys.

The colours in the library are defined as RGB. The target colourspace is RGB, profiled as AdobeRGB.

The destination is the website of a large retail store chain, who post these colour swatches on product pages. The retail store have very specific requirements and has always asked for JPEG, Maximum, 1000x1000px (perhaps they are unaware of the potential benefits of using other formats for solid colours?)

I have attached a screen-grab of one of the colour library groups - "VisionAiry_Gel_Lipstick" showing 28 colours within it. The individual groups have been imported from a .csv file containing colour names and RGB values, as specified by the client, using a script that creates an .ase file that is imported into Illustrator and Photoshop. I have also attached the jpeg file that was generated, from this same colour group - manually saved and named as "VisionAiry_Gel_Lipstick_Pixel_Pink.jpg"

Let me know if I should supply any other details.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

A point on terminology:

RGB is a Color Mode, not a Color Space. 

Adobe RGB is a Color Space. 

 

»The individual groups have been imported from a .csv file containing colour names and RGB values,«

Then might using the original csv file not be easier? 

Could you provide it (or a part of it)?  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

Thanks for clarifying my understanding of the terminology.

I have attached the .csv file that was used to create the Lipstick colour .ase.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 22, 2021 May 22, 2021

Copy link to clipboard

Copied

Hi All

Has anyone come up with any suggestion for this to be scripted?

Also, I am a little confused by the terminology comment. My OS actually records the info for my Jpegs as "Colour Space" RGB and "Colour Profile" AdobeRGB - see attached screen-grab. Is the OS using the wrong terms?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

// create 1000x1000 jpgs with colors based on csv;
// 2021, use it at your own risk;
// define the path of the file;
var thePref = File.openDialog ("please select file", '*.csv', false);
var thePath = File (thePref).path;
var theName = File (thePref).name.split(".")[0];
// check for existence of file:
if (File (thePref).exists == true) {
var theText = readPref (thePref);
theText = theText.split("\r");
// create documents;
for (var m = 1; m < theText.length; m++) {
  var thisOne = theText[m].split(",");
  var thisName = thisOne[0].slice(1);
	var newDoc = app.documents.add(UnitValue (1000, "px"), UnitValue (1000, "px"), 300, theName+"_"+thisName, NewDocumentMode.RGB);
  newDoc.convertProfile ("Adobe RGB (1998)", Intent.RELATIVECOLORIMETRIC, true, true);
  createSolidColorLayer(thisOne[1], thisOne[2], thisOne[3]);
  newDoc.flatten();
  saveJpg (activeDocument, thePath, theName+"_"+thisName, 12)
}
};
////// read prefs file //////
function readPref (thePath) {
  if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = new String;
    for (var m = 0; m < file.length; m ++) {
      theText = theText.concat(file.readch());
      };
    file.close();
    return String(theText)
    }
  };
////// make solid color layer //////
function createSolidColorLayer (theRed, theGreen, theBlue) {
  // create solid color layer;
  // =======================================================
  var idMk = charIDToTypeID( "Mk  " );
      var desc8 = new ActionDescriptor();
      var idnull = charIDToTypeID( "null" );
          var ref6 = new ActionReference();
          var idcontentLayer = stringIDToTypeID( "contentLayer" );
          ref6.putClass( idcontentLayer );
      desc8.putReference( idnull, ref6 );
      var idUsng = charIDToTypeID( "Usng" );
          var desc9 = new ActionDescriptor();
          var idType = charIDToTypeID( "Type" );
              var desc10 = new ActionDescriptor();
              var idClr = charIDToTypeID( "Clr " );
                  var desc11 = new ActionDescriptor();
                  var idRd = charIDToTypeID( "Rd  " );
                  desc11.putDouble( idRd, theRed );
                  var idGrn = charIDToTypeID( "Grn " );
                  desc11.putDouble( idGrn, theGreen );
                  var idBl = charIDToTypeID( "Bl  " );
                  desc11.putDouble( idBl, theBlue );
              var idRGBC = charIDToTypeID( "RGBC" );
              desc10.putObject( idClr, idRGBC, desc11 );
          var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
          desc9.putObject( idType, idsolidColorLayer, desc10 );
      var idcontentLayer = stringIDToTypeID( "contentLayer" );
      desc8.putObject( idUsng, idcontentLayer, desc9 );
  executeAction( idMk, desc8, DialogModes.NO );
  return activeDocument.activeLayer
  };
////// save jpg copy //////
function saveJpg (thedoc, docPath, basename, theQuality) {
// check for existing file 
  if (File(docPath+'/'+basename+'.jpg').exists == true) {
    var theConfirm = confirm("overwrite existing file");
    if (theConfirm == false) {return false}
    };
  try {
  var jpegOptions = new JPEGSaveOptions();
  jpegOptions.quality = theQuality;
  jpegOptions.embedColorProfile = true;
  thedoc.saveAs((new File(docPath+'/'+basename+'.jpg')),jpegOptions, false);
  thedoc.close(SaveOptions.DONOTSAVECHANGES)
  }
  catch (e) {docName+" failed"}
  };

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Absolutely brilliant! Tested and works perfectly.

There is no way I could have written something like that myself. Thank you so much.

I have advised the client of the "pitfalls" of using .jpg, not the least of which appears to be some minor colour distortion from the actual RGB values. Unfortunately, they have reiterated their insistence on using that file type, even though I have pointed out the advantages of a .png file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

You may want to amend the target location, saving right beside the csv-file may not make that much sense in the actual production process. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

LATEST

Noted.

And thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines