Tiff to JP2 Batch Macro Help

Hi, I have been trying to find a macro that will allow me to batch convert a number of tiff images to jp2 from a directory that i designate to it, and then put it in a folder that I also tell it to go to.

However I haven’t found any good results. I tried the recorder to record my steps, but it doesn’t work correctly when I try to run in the Batch Macro tool, because it asks for a Output format that doesn’t include JP2. It will convert the first file I designate, but then it converts the other files to the output format that is in the batch macro tool.

I also need to tell it to give me a normal Jp2, not a Lossy Jp2.

Can someone help me with this macro? I haven’t really made any progress yet…

Dir1 = getDirectory("Choose Source Directory");
Dir2 = getDirectory("Choose Destination Directory");

list = getFileList(Dir1);
setBatchMode(true);

???????

If you managed to save one file in the format you wanted, then you can reproduce the same behavior for a whole folder using this macro example.

Run the Macro recorder to run a single TIF file and convert to JP2. Using instructions I made generic and to allow for iterating many files.

Ran the macro, but it still is not working. I get the dialog boxes to set my input directory, and my output directory. When after I set the output directory, I get a Opener message that says, File is not in a supported format, a reader plugin is not available, or it was not found. Then it says output directory path + \input + filename

input = getDirectory("Choose Source Directory");
output = getDirectory("Choose Destination Directory");

setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
	action(input, output, list[i]);
function action(input, output, filename) {
	open("input + filename");
		run("Bio-Formats Exporter", "save=[output + filename] compression=jpeg-2000");
		close();
}

You almost got it. You just need to be careful with the string quotes and file extensions:

input = getDirectory("Choose Source Directory");
output = getDirectory("Choose Destination Directory");

setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
    action(input, output, list[i]);

function action(input, output, filename) {
    open( input + filename );
    extensionIndex = lastIndexOf( filename, "." );
    newFileName = substring( filename, 0, extensionIndex ) + ".jp2";
    run("Bio-Formats Exporter", "save=[" + output + newFileName + "] compression=JPEG-2000");    
    close();
}

Great news, this was my first try at writing code but with your help it is now working. Thank you for all your help it is greatly appreciated!!! :+1::+1:

In case you missed it, there is a template in the Script Editor for doing batch processing in this manner. See the Batch Processing page for details.

1 Like

Dear Dr. Ignacio,

I modified the above macro for my purpose.
I wanted to overlay file name on the image and save the file as jpg format.
However, my macro stops at “Overlay.drawString(filename, x, y);” commands.
I would appreciate, if you could kindly let me know what is wrong.

Best regards,
Takeshi

input = getDirectory("Choose Source Directory");
output = getDirectory("Choose Destination Directory");

fontSize = 40; 
x = 10; 
y = fontSize; 
setColor("white") 
setFont("SansSerif", fontSize);
setBatchMode(true);
list = getFileList(input);

for (i = 0; i < list.length; i++)
        action(input, output, list[i]);

function action(input, output, filename) {
    open( input + filename );
    extensionIndex = lastIndexOf( filename, "." );
    //newFileName = substring( filename, 0, extensionIndex ) + ".jp2";
    Overlay.drawString(filename, x, y); 
    Overlay.show; 
    saveAs("Jpeg", output);
    close();
}

Good day Takeshi,

you are to move

setColor("white");
setFont("SansSerif", fontSize);

into the function.

HTH

Herbie

1 Like

Dear Dr. Herbie,

I moved the below lines into function, however my macro stops at
setFont(“SansSerif”, fontSize);

I still can’t find what is wrong with my macro. Could you tell me how to solve this problem one more time?

Best regards,
Takeshi

fontSize = 40; 
x = 10; 
y = fontSize; 
setColor("white") 
setFont("SansSerif", fontSize); 

input = getDirectory("Choose Source Directory");
output = getDirectory("Choose Destination Directory");

list = getFileList(input);	
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);
        setBatchMode(true);		

function action(input, output, filename) {
    open( input + filename );
    //extensionIndex = lastIndexOf( filename, "." );
    fontSize = 40; 
    x = 10; 
    y = fontSize; 
    setColor("white") 
    setFont("SansSerif", fontSize); 
    name = getInfo("image.filename");
    title = getTitle(); 
    Overlay.drawString(name, x, y); 
    Overlay.show; 
    saveAs("Jpeg", output+title);
    close();
}

Memory * 70MB of 512MB (13%)
nImages() 1
getTitle() "Composite 522,523.jpg"
input "D:\UserData\07733\Desktop\JPEG_Folder"
output "D:\UserData\07733\Desktop\JPEG_Folder"
list array[1]
i 0
filename "Composite 522,523.jpg"
output "D:\UserData\07733\Desktop\JPEG_Folder"
input "D:\UserData\07733\Desktop\JPEG_Folder"
fontSize 40
x 10
y 40

Dear Takeshi,

there is a semicolon missing in your code after " setColor(“white”) ".

For me the code

input = getDirectory("Choose Source Directory");
output = getDirectory("Choose Destination Directory");

list = getFileList(input);	
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);
        setBatchMode(true);		

function action(input, output, filename) {
    open( input + filename );
    //extensionIndex = lastIndexOf( filename, "." );
    fontSize = 40; 
    x = 10; 
    y = fontSize; 
    setColor("white");
    setFont("SansSerif", fontSize); 
    name = getInfo("image.filename");
    title = getTitle(); 
    Overlay.drawString(name, x, y); 
    Overlay.show; 
    saveAs("Jpeg", output+title);
    close();
}

is running fine now.

HTH

Herbie

2 Likes

Dear Dr. Herbie

I really appreciate your kind support. and also I am so sorry for my careless mistake.
I could not notice this mistake.

I will study macro and JAVA over from the basics.

Sincerely yours,
Takeshi