Calculating surface and storing as Excel file - imagej

I'm new to imageJ and I am trying to analyze several images:
I have a code that can analyze color threshold for a set of images in a directory and store them separately:
input = "/m_3/ImageJ/test_folder/";
output = "/m_3/ImageJ/finished2/";
function action(input, output, filename) {
open(input + filename);
run("Set Scale...", "distance=872 known=9 pixel=1 unit=cm");
run("Color Threshold...");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
.
.
.
// Colour Thresholding-------------
saveAs("Jpeg", output + filename);
close();
}
setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
action(input, output, list[i]);
setBatchMode(false);
Now I want to calculate the area of the newly saved images and this should work with the function measure..
run("Measure");
How can I store the calculations in .xls or .csv -files?
Is it also possible to calculate the area of all the files in one directory and store the results in just one .xls or .csv -file?

Have a look at the explanation how to apply a common operation to a complete directory on the Fiji wiki. You can open each of the newly save images, set a threshold, measure, and close each image as follows:
open("/path/to/your/image.jpg");
setAutoThreshold("Default");
run("Measure");
close();
and paste that code into the Process Folder template (Templates > IJ1 Macro > Process Folder) of the script editor.
The results table can afterwards be saved as an .xls file via File > Save As... or:
saveAs("Results", "/path/to/your/file.xls");

Related

The file is not open in imagej

I wrote the code as follows in ImageJ.
But the image window is not created in open.
I want to load all the tif files into the folder and see them.
The problem is the line setBatchMode(true);
If you delete that line, or set it to false, your script will open all the files in the folder in a way that you can see them. BatchMode allows a script to run without drawing the window and is intended for use in an automated script.
dir1 = getDirectory("Choose Source Directory");
list = getFileList(dir1);
for (i = 0; i < list.length; i++) {
showProgress(i+1,list.length);
filename = dir1 + list[i];
if(endsWith(filename, "tif"));
{
open(filename);
}
}

How to resave images existing on disc in DM 3.4 (Force a replacement.)

Try to do some image edit (add text and scale bar) and then automatically save/replace the original image, the codes work well with DM 2.0 and previous verison, but it does not work with 3.4 and 3.0, it shows " the process can not access the file because it is being used by another process". Is there any other way to do this with 3.4 and 3.0? thx
image front:=getfrontimage()
string imgname=getname(front)
string handler = "Gatan 3 Format"
ImageDocument doc = GetFrontImageDocument()
string thispath=pathconcatenate(currentdirectory, imgname)
doc.ImageDocumentSaveToFile( handler, thispath)
You can not save a file to a file-name of a currently opened file. Instead, you want to "re-save" your opened file. The easiest way to do that would be
image img := GetFrontImage()
img *= -1 // Just something to modify the image
img.Save()
If you want to do the same with an ImageDocument instead, it would be
imageDocument doc = GetFrontImageDocument()
doc.ImageDocumentSave(1)
The parameter in the command can either be:
0 = Always save, never prompt (Use linked location or folder of DigitalMicrograph.exe if not linked.)
1 = Save without prompting, if already linked to file, else prompt
2 = Always prompt
The following commands can also be useful when working with files and imageDocuments:
image img := GetFrontImage()
imageDocument doc = img.ImageGetOrCreateImageDocument()
Result( "\n Image ["+img.ImageGetLabel()+"] " )
if ( doc.ImageDocumentIsLinkedToFile() )
Result( "currenlty linked to:" + doc.ImageDocumentGetCurrentFile() )
else
Result( "not linked to a file." )

How to batch process ImageJ macro within a specific folder within multiple directories

I have a nested file structure where a parent folder contains multiple folders of different types of data. I am using an ImageJ macro script to batch process all of the image files within one of those folders. I currently need to process each folder separately, but I would like to batch process over the folders. I have looked up some batch processing of multiple folders, but it appears that the code is processing all folders and files within all of the folders. I only need to process one folder within each directory (all named the same). The images come from the instrument without any metadata, so the files are saved as such to separate the experiments, where all data for the experiment is contained within the parent folder. Also, I have two different scripts that I need to run, one after the other. It would be great if I could merge those, but I am don't know how to do that either.
An example of the structure is:
Experiment1/variable1/processed
Experiment1/variable2/processed
I am currently running my macro on each of the "processed" folders individually. I would like to batch each "processed" folder within each of the "variable" folders.
Any help would be greatly appreciated, I am really new to coding and am just really trying to learn and automate as much as possible.
Thank you!
Did you try the batch processing scripts you came across? Reading the batch processing example which is provided with ImageJ leads me to believe it would work for your example. If you haven't tested it, you should do so (you can put in a command like "print(list[i])" in the place of your actual macro while you test that you've got the file finding section working.
To merge two different scripts, the simplest option would be to make them individual functions. i.e.:
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
processOtherWay(input, output, list[i]);
}
}
function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
function processOtherWay(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
If the goal isn't to run them on the exact same image, then again make them standalone functions, and have the folder sorting section of the script be in two parts, one for function 1, one for function 2.
You can always just take the code you have and nest it in another for loop or two.
numVariables = ;//number of folders of interest
for(i = 1; i <= numVariables; i++) //here i starts at 1 because you indicated that is the first folder of interest, but this could be any number
{
openPath = "Experiment1/variable" + i + "/processed";
files = getFileList(openPath);
for(count = 0; count < files.length; count++) //here count should start at 0 in order to index through the folder properly (otherwise it won't start at the first file in the folder)
{
//all your other code, etc.
}
}
That should just about do it I think.

Batch save all opened files in GIMP .xcf

I have a series of manually edited images and layers in GIMP, each set is in a single tab. All want to save all of them into different .xcf's.
I am aware of some scripts to export them as images (like this one), but I want to save them as .xcf, not export the images. Moreover, I would like to have them in a single folder, so that I can load them all in the future.
Is there any script to do this?
You can save all projects using this code bellow
dirname = “Path of file”
path = “File name”
imgs = gimp.image_list();
num = 0;
for img in imgs:
for layer in img.layers:
fullpath = os.path.join(path, dirname+str(num)+'.xcf');
pdb.gimp_xcf_save(0, img, layer, fullpath, fullpath);
num += 1;
Or, if you can install complete plugin in https://github.com/Tushn/GIMP-Plugins/blob/master/src/saveproject.py
Instructions for install in https://github.com/Tushn/GIMP-Plugins
If you want open all file, so it’s enough that you mark all xcf in directory and enter in GIMP (click right buttom mouse, case your GIMP is not default file).

How to zip PDF and XML files which are in memorystreams

I'm working with VS2015 and ASP.Net on a webservice application which is installed in the AWS cloud.
In one of my methods i got two files, a PDF and a XML.
These files just exist as instances of type MemoryStream.
Now i have to compress these two "files" in a ZIP file before adding the zip as attachment to an E-mail (class MailMessage).
It seems that i have to save the memorystreams to files before adding them as entries to the zip.
Is ist true or do i have another possibility to add the streams as entries to the zip?
Thanks in advance!
The answer is no.
It is not necessary to save the files before adding them to the stream for the ZIP file.
I have found a solution with the Nuget package DotNetZip.
Here is a code example how to use it.
In that example there two files which only exist in MemoryStream objects, not on a local disc.
It is important to reset the Position property of the streams to zero before adding them to the ZIP stream.
At last i save the ZIP stream as a file in my local folder to control the results.
//DotNetZip from Nuget
//http://shahvaibhav.com/create-zip-file-in-memory-using-dotnetzip/
string zipFileName = System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".zip";
var zipMemStream = new MemoryStream();
zipMemStream.Position = 0;
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
textFileStream.Position = 0;
zip.AddEntry(System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".txt", textFileStream);
xmlFileStream.Position = 0;
zip.AddEntry(xmlFileName, xmlFileStream);
zip.Save(zipMemStream);
// Try to save the ZIP-Stream as a ZIP file. And suddenly: It works!
var zipFs = new FileStream(zipFileName, FileMode.Create);
zipMemStream.Position = 0;
zipMemStream.CopyTo(zipFs);
zipMemStream.WriteTo(zipFs);
}

Resources