Opening Text File in Color


#1

Hello, I currently have .txt files that have values of 1 or 0. I have 18 different .txt files and want to open them in imagej and apply a different color to each one. For example, the first .txt file will be red for every value of 1 and remain black for every value of 0. The next will be orange for every value of 1 and black for every value of zero and so on. I am able to change values of 1 to certain numbers if need be in order to open them in imagej, however I am unsure of what color code would work best (hex, rgb, etc.). I am able to change every ‘1’ to a certain hex code however when inputting into imagej it does not read as a black image with whatever color code is being used. Any help with this would be greatly appreciated!


#2

Good day Ruth,

let’s see what can be done with an ImageJ macro …

Here is a demo macro that colors the ones of a binary text file in red and saves it as a RGB-image:

path = getDirectory("home");
run("Text Image... ", "open=" + path + "bin-1.txt" );
run("Red");
run("RGB Color");
run("Save", "save=" + path + "red.tif");
  1. Here is a sampe text file. Please copy it to a text editor and save it named “bin-1.txt” (without quotation marks) to your home directory.

0 1 1 0 0 0 0 0 1 0 0 1 0 1 1 1
0 1 0 1 0 1 1 1 0 0 0 1 0 0 0 1
0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1
1 1 1 0 1 0 0 0 0 1 0 1 0 0 1 0
0 0 0 1 1 1 1 1 1 0 1 0 0 0 1 1
0 1 0 1 1 0 1 0 1 1 1 1 0 1 0 1
1 0 1 1 1 0 1 1 1 0 0 0 0 1 0 1
1 0 1 1 1 0 0 0 0 1 1 1 1 1 1 1
0 0 1 1 1 1 0 0 0 1 0 1 0 0 0 1
1 0 0 1 0 1 0 1 1 1 1 1 0 0 0 1
0 1 0 1 1 1 0 0 0 0 1 0 0 0 1 1
0 0 1 0 1 1 0 1 1 1 0 1 0 1 1 1
1 0 1 1 1 0 1 0 0 1 0 1 0 0 1 0
1 1 1 0 1 1 1 1 1 1 0 1 0 1 0 0
0 1 1 1 0 1 0 1 0 0 1 0 0 1 1 0
0 1 0 1 1 0 1 1 1 1 0 1 1 1 0 1

  1. Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

  2. A RGB-image named “red.tif” will be saved in your home directory.

To generalize this to other colors is rather easy if you use the macro recorder of ImageJ and perhaps consult:
https://imagej.nih.gov/ij/developer/macro/functions.html

Regards

Herbie


#3

Thank you so much for your help! This worked. Now for one step further, is it possible to replace the run(“red”) code with RGB values. I was able to create images with red, green, and blue, however we will eventually need a bunch of images with a bunch of different colors all with different RGB values, so I am wondering if there is a way to do this same process but open the image with a specified RGB value rather than just red. Thank you again!


#4

Good day Ruth,

the code line “run(“Red”);” calls a LUT. You can create your own LUTs with the desired colors and name them appropriately.

Please study the ImageJ docs, especially Section 28.15
https://imagej.nih.gov/ij/docs/guide/146-28.html#toc-Subsection-28.15
and Subsection 28.5.7
https://imagej.nih.gov/ij/docs/guide/146-28.html#sub:Edit-LUT
and most important
https://imagej.nih.gov/ij/plugins/lut-editor.html

You may start with inspecting an existing LUT.

Good luck

Herbie

EDIT:
here is the code for Magenta:
path = getDirectory(“home”);
run("Text Image… ", “open=” + path + “bin-1.txt” );
run(“Magenta”);
run(“RGB Color”);
run(“Save”, “save=” + path + “magenta.tif”);

Magenta is already defined, as are Green, Blue, Cyan, Yellow


#5

@rbarrow

Too - once you have your three color tif images - you can use the Stack to RGB call under Image > Color > Stack to RGB to make your RGB image.

That tool “Converts a two or three slice stack into an RGB image, assuming that the slices are in R, G, B order. The stack must be 8-bit or 16-bit grayscale. Also converts composite images (e.g., File▷Open Samples▷HeLa Cells (1.3M, 48-bit RGB)) into RGB.

eta :slight_smile:


#6

Good day Ellen,

sorry but I can’t connect your suggestion to Ruth’s original question.

I think she is quite happy with what I’ve suggested and only wants different coloring which is easily achieved.

Regards

Herbie


#7

no worries. just threw it out there in case she found that functionality helpful down-the-road. for sure - your answer is what she originally needed. so it’s all good! (i’m also taking a lot of cold medicine at the moment… so my brain might be a bit fuzzy!)

eta :slight_smile:


#8

Good day Ruth,

in case you struggle with defining LUTs for your pupose, here are the steps:

  1. Open a new 8bit image, e.g. by using the macro line:
    newImage("Untitled", "8-bit ramp", 16, 16, 1);
  2. Go to the Menu-entry “Image >> Color >> Edit LUT…”
  3. In the LUT Editor dialog

    click in the little square at the lower right.
  4. A Color Editor opens that allows you to create the desired color, here “Spring Green”.
    ColorEditor
    Close the editor with OK.
  5. In the LUT Editor dialog click “Save” and save as “255_SpringGreen.lut” to the luts-folder of ImageJ.
    Close the editor with OK.

You’re done.

Background information:
For your purpose it is sufficient to set the desired color for the LUT-entry 255. (All other entries, except the LUT-entry 0 that needs to be black, are irrelevant.)
The reason for this is that if you open a 0,1-binary text image it is displayed as a 32bit image and if you assign a LUT to it, value 1 is regarded as value 255.

HTH

Herbie