Skip to main content
rechmbrs
Inspiring
March 6, 2020
Question

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

  • March 6, 2020
  • 1 reply
  • 3251 views

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?page=1 

 

******  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

This topic has been closed for replies.

1 reply

Geppetto Luis
Legend
March 6, 2020

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

rechmbrs
rechmbrsAuthor
Inspiring
March 6, 2020

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

Geppetto Luis
Legend
March 7, 2020

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");
}
}
}