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

Example of Image to Text ExtendScript code (Revised 06 Mar 2020)

Engaged ,
Mar 06, 2020 Mar 06, 2020

Copy link to clipboard

Copied

The Extendscript code below will take a png file and output it as a txt file for you to copy the contents into your Scriptui code where an image is desired.

This is a revision of https://community.adobe.com/t5/photoshop/example-of-image-to-text-extendscript-code/m-p/10965512?pag... 

 

******  See below for more automated version by geppettol66959005  ******

 

 

 

// extendscript to copy image to text for use in storing info within the jsx file
// RONC 06 Mar 2020
// image2txt2.jsx
/**
* @@@BUILDINFO@@@ image2txt2.jsx !Version! Fri Mar 06 2020 03:31:42 GMT-0600
*/
/* revision of previous post with use of suggestion by r-bin */
/* scriptui options image and iconbutton use image input which is most often from and image
   file.  It is also possible to use an embedded version of the image data as part of those options
   this script is designed to assist in creating the file of text to be embedded.  Once the file
   is created, this new file can be opened in a text editor like Notepad in Windows and then using
   a simple copy/paste operation to put the file information into the source code of the the script
   where the embed is desired.  Ctrl-A followed by Ctrl-C will copy the information in the text 
   editor and Ctrl-V will paste into the source code.
*/

// Instead of using file name in image or iconbutton scriptui calls.:
//  var iconFile = new File ("C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/TestKey2.png");
// Use:
//  Contents of the output txt file which is var iconFile = "...  ....";

//  Must use either / or \\ rather than \ in file info !!!

// these eight parameters control the input, changes made, and output
//
var ierr = 0;
var inputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var inputFileName = "ui-bg_diagonals-thick_18_b81900_40x40RC8";
var inputFileExt = ".png";

var varText = "var iconFile = ";  //  set to "" if not desiring the jsx info
var varEnd = ";";  //  set to "" if not desiring the jsx info

var outputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var outputFileName = "ui-bg_diagonals-thick_18_b81900_40x40RC8_3x";
var outputFileExt = ".txt";

var i;

// read image
var infile = File(inputFilePath + inputFileName + inputFileExt);
infile.open("r");
infile.encoding = "binary";
var temp = infile.read();
infile.close();

// check input if OK create output
var len = temp.length;
if (len === 0)
{
    alert("input file error - zero length");
    ierr = 1;
}
else
{
    var temp2 = "var iconFile = String.fromCharCode(";

    for (i = 0; i < len; i++) 
    {
        temp2 += (i ? "," : "") + temp.charCodeAt(i);
    }
    temp2 += ");\n";

    // output converted to txt
    var outfile = File(outputFilePath + outputFileName + outputFileExt);
    outfile.open("w");
    outfile.write(temp2);
    outfile.close();
    ierr = 0;
}

if (ierr === 0)
{
    alert("Input file:\n" + inputFilePath + inputFileName + inputFileExt + "\n\nwritten to Output file:\n" + outputFilePath + outputFileName + outputFileExt);
}

 

 

RONC

TOPICS
Actions and scripting , SDK

Views

2.9K

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
Advocate ,
Mar 06, 2020 Mar 06, 2020

Copy link to clipboard

Copied

Couldn't RONC simplify the script with prompt window that selects the file?

just as it is proposed every time one has to convert image into numbers he has to intervene and insert the data in the .jsx 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
Engaged ,
Mar 06, 2020 Mar 06, 2020

Copy link to clipboard

Copied

geppettol66959005,

 

We need someone else who knows how to insert what you are asking for.  I gleaned this information from a question I asked and decided to share what I had found.  Maybe you could add the file info interface?

 

RONC

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
Advocate ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

RONC
today I had some free time and I created this
I hope it will be useful to you

 

var win = new Window("dialog"); 
    win.text = "CONVERT .PNG - DECODE .TXT"; 
    win.orientation = "column"; 
    win.alignChildren = ["center","top"]; 
    win.spacing = 10; 
    win.margins = 16; 

// GROUP1
// ======
var group1 = win.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var button1 = group1.add("button", undefined, undefined, {name: "button1"}); 
    button1.text = "CONVERT PNG"; 

var button2 = group1.add("button", undefined, undefined, {name: "button2"}); 
    button2.text = "CLOSE"; 
    
button1.onClick = function () {
win.close();
CONVERT_FILE_PNG()
}
    
button2.onClick = function () {
win.close();
}

win.show();

function CONVERT_FILE_PNG()
{

fileObj = File.openDialog("SELECT THE FILE TO BE PROCESSED .PNG :");   
Decode(fileObj)   

////////////////////////////////////////////////////////////////////////////////////////////  
function Decode(file)   
    {       
    try {  
            if (!file.fsName.match(/\.(PNG|png)$/i))
            {               
                alert("ERROR!!!\nYOU HAVE SELECTED AN INVALID FILE\n\nYOU MUST SELECT ONLY FILES.PNG?");
            return;  
            }  
 
CONVER_ICON()


	if (fileObj != null){
}
  // FINE SCRIPT
        return true;  
        }  
    catch (e) { alert(e); return false; }  
  }  


var ierr = 0;

function CONVER_ICON()
{
var inputFilePath = "" + fileObj;

var varText = "var iconFile = ";  //  set to "" if not desiring the jsx info
var varEnd = ";";  //  set to "" if not desiring the jsx info

var outputFilePath = "" + inputFilePath ;
 var outputFileName = "" ;
var outputFileExt = ".txt";

var i;

// read image
var infile = File(inputFilePath);
infile.open("r");
infile.encoding = "binary";
var temp = infile.read();
infile.close();

// check input if OK create output
var len = temp.length;
if (len === 0)
{
    alert("ERRORE!!!");
    ierr = 1;
}
else
{
    var temp2 = "var iconFile = String.fromCharCode(";

    for (i = 0; i < len; i++) 
    {
        temp2 += (i ? "," : "") + temp.charCodeAt(i);
    }
    temp2 += ");\n";

    // output converted to txt
    var outfile = File(outputFilePath + outputFileName + outputFileExt);
    outfile.open("w");
    outfile.write(temp2);
    outfile.close();
    ierr = 0;
}

if (ierr === 0)
{
    alert("Finished");
}
}
}

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
Engaged ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

WWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWW.

 

Extremely helpful.  Thanks.

RONC

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
Advocate ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

LATEST

Thanks for that script. One issue i do see with this, is that i cant update the icons with .onchange function.

When icons are loaded from disk using a PNG i can swap the image. Doesnt seem to work with any binary approach i tried. Any idea why that is?

 

The image changes when i pick different method from the dropdown.

normally this works thmb.image = thumb1 wheres as thmb1 is a var from external file. BUt using the vars described in the generated file, first one loaded in is the only iotion i get.

 

I also tried the dialog website to create panels. This one can also generate binary image, but uses File.decodo(VARTHUMB) but also get stuck at first image?

 

That website is so nice; https://scriptui.joonas.me/ 

Its a use time saver and super nice to quickly mockup ideas

 

EDIT

Perhaps im crazy, but i just checked the generated files, it outputs exact same code for 3 different icons for me. Im usign windows PS 22

 

EDIT2

well duhh im using icons which are partly the same. it does work, but not as smooth as with loaded PNG files. It only updates when use File.decode() and i also need to select another option first then reselect the earlier option otherwise it wont change. Kinda weird, not usefull in that manner. I was really hoping i could dump those external files

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