• 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

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

 

// extendscript to copy image to text for use in storing info within the jsx file
// RONC 03 Mar 2020
// image2txt.jsx
/**
* @@@BUILDINFO@@@ image2txt.jsx !Version! Tue Mar 03 2020 05:52:11 GMT-0600
*/

// Instead of using:
//  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 / rather than \ in file info !!!

// these eight parameters control the input, changes made, and output 
//
var inputFilePath = "C:/Users/RECHM/Dropbox/$$-SYNC/$$-Scripts/__SC-Save/";
var inputFileName = "TestKey2";
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 = "TestKey2";
var outputFileExt = ".txt";


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

if (temp.length === 0)
{
    alert("input error");
}

// output converted to txt
var outfile = File(inputFilePath + "work" + ".txt");
outfile.open("w");
outfile.write(temp.toSource());
outfile.close();

// read work file
var infile2 = File(inputFilePath + "work" + ".txt");
infile2.open("r");
infile2.encoding = "binary";
temp = infile2.read();
infile2.close();

infile2.remove();

// remove beginning and trailing characters
var temp2 = temp.replace("(new String(", varText);
temp = temp2.replace("))", varEnd);

// output to final txt file ready for copying to jsx code
var outfile2 = File(outputFilePath + outputFileName + outputFileExt);
outfile2.open("w");
outfile2.write(temp);
outfile2.close();

alert("Input file:\n" + inputFilePath + inputFileName + inputFileExt + "\nwritten to Output file:\n" + outputFilePath + outputFileName + outputFileExt);

 

Please comment.

 

RONC

TOPICS
Actions and scripting , SDK

Views

2.3K

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
LEGEND ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

You may get rid of:

 

var temp2 = temp.replace("(new String(", varText);
temp = temp2.replace("))", varEnd);

 

 

Then in final script use the content of .txt file:

 

str = (new String("yourVeryLooooooooooooooooooooooooooooooooooooooongPNGcode"));
(win = new Window('dialog')).add('iconbutton', undefined , '' + str), win.show()

 

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 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

K,

 

The two statements 

var temp2 = temp.replace("(new String(", varText);
temp = temp2.replace("))", varEnd);

 remove the string "(new String(" and "))" that surround the image.  Those strings are added the toSource() function.   In my test, I had to remove them for reading an image as text by Scriptui.

 

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
LEGEND ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

For same effect you don't have to remove them, just copy to .jsx file and assign to variable.

 

Not as "(new String("PNGcode"))" but (new String("PNGcode")) (so without quotation marks).

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 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

I'm not going to change anything as it works fine as is.  If there is a bug please point it out.

 

Can I in the first part diagnose that the input is 16 bit png?  I would like to give the user a message and then cancel the run.

 

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
LEGEND ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

You wanted comments, so I did mine, and still I think although your version works then it's better to make it simpler, the way I offered, plus I guess removing surraounding part had to be earlier done by someone who didn't know better method. That's my conclusion from your other posts, when at least once you resigned from 'with' statement provided by r-bin.

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
LEGEND ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

I have code to convert files to base64 and then back. Nothing special, easy to do with system calls. There is no real need for it though.

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 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

Lumigraphics,

Thanks for the response.

I put this together based on recommendations I found in the docs for how to do this.  I combined the manual step of removing the two strings while copying from the text editor and pasting into the Scriptui source code you are building.   It is a staight select all, copy, and paste now. 

You might explain how base64 fits into this as it is not mentioned in any of the docs that I read.

 

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
Community Expert ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

Do not hesitate to post a link to this thread in the exchange forum as well !

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
LEGEND ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

I haven't had time but I'm going to release some functions as .jsxinc files so they can easily be added to a project. One of those is a Base64 encode/decode module. I have seen self-contained ones but mine use system calls.

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
Enthusiast ,
Feb 11, 2023 Feb 11, 2023

Copy link to clipboard

Copied

LATEST

Seems there is an easier way: use File encode/decode. To give credit where credit is due, I got the idea from the ScriptUI Dialog Builder website (https://scriptui.joonas.me/)

This encodes the binary then chunks it so the lines aren't too long (VS Code complains). And assign the result to 'var image =' while at it. Chunking gets tricky if using toSource() but works fine with URL encoded content. Copy the result and paste into a script. Then to use, drop in the variable 'image' (or whatever you want to call it)

 

var button = g.add("iconbutton", undefined, image);

 

// Encode Binary.jsx
var file = File.openDialog();
if (file) {
    file.open("r");
    file.encoding = "binary";
    var binary = file.read();
    file.close();
    var encoded = File.encode(binary);
    var chunks = encoded.match(/.{1,78}/g);
    file = new File(file.fullName.replace(/\..+$/, ".txt"));
    file.open("w");
    file.write("var image = File.decode(\r");
    for (var i = 0; i < chunks.length - 1; i++) {
        file.write("\"" + chunks[i] + "\" +\r");
    }
    file.write("\"" + chunks[i] + "\"\r);\r");
    file.close();
}
alert("Done");

 

William Campbell

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