Copy/paste/makeRectangle issue

Hello people,

I am asking about this issue which is driving me nuts!

Below there are few lines of codes I got with the macro recorder, where I am opening a stack called “image.tif”, I am averaging the frames, getting a line profile from the averaged stack and pasting in a new .jpeg file called “Merged”, the averaged stack and the line profile next to each other.

open("C:/Users/Mike/Desktop/image.tif");
selectWindow("C:/Users/Mike/Desktop/image.tif");
run("Z Project...", "projection=[Average Intensity]");
newImage("Merged", "RGB white", 1048, 512, 1);
selectWindow("AVG_C:/Users/Mike/Desktop/image.tif");
//setTool("line");
makeLine(1, 102, 512, 102);
run("Plot Profile");
selectWindow("AVG_C:/Users/Mike/Desktop/image.tif");
run("Copy");
selectWindow("Merged");
run("Paste");
makeRectangle(0, 0, 512, 512);
selectWindow("Plot of AVG_C:/Users/Mike/Desktop/image");
run("Copy");
selectWindow("Merged");
run("Paste");
makeRectangle(512, 0, 1079, 420);

The problem I have is with the copy/paste/makeRectangle. When I run the macro, the selections get pasted in the centre of the “Merged” file instead in the position of the rectangle. I don’t understand why, as those lines come from the Macro recorder, where after pasting the images, dragging them in the “Merged” file created the makeRectangle command.
Also, I tried setSelectionLocation(x, y); but doesn’t help too.

Any help is well appreciated!

Thanks!

Call makeRectangle(x,y,width,height) just before run(“Paste”), where ‘x’ and ‘y’ specify the desired location and ‘width’ and ‘height’ are the width and height of the selection. Here is an example:

  run("Boats");
  makeRectangle(135, 167, 120, 200);
  run("Copy");
  newImage("Untitled", "8-bit black", 400, 400, 1);
  makeRectangle(0,0,120,200);
  run("Paste");

Thank you Wayne.
However, even if your suggestion worked (still I don’t see why as what you do with the macro recorder should stay the same when you run the macro…) when I run this chunk of code in my larger macro:

                 newImage("Homogeneity Lines Profiles"+list2[a], "RGB white", 1120, 512, 1);
                 selectWindow("AVG_"+list2[a]); 
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);                                
                 makeRectangle(0, 0, 512, 512);
                 run("Paste");
                 selectWindow("Profile line 1");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);               
                 makeRectangle(515, 174, 595, 165);
                 run("Paste");               
                 close("Profile line 1");
                 selectWindow("Profile line 2");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);              
                 makeRectangle(515, 1, 595, 165);
                 run("Paste");
                 close("Profile line 2");
                 selectWindow("Profile line 3");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);                 
                 makeRectangle(515, 348, 595, 165);
                 run("Paste");
                 close("Profile line 3");

I still have the line profiles centered and overlapped, please see attached.

What might be the issue?

Please provide a small but runnable macro that reproduces the problem. This example works as expected:

newImage("Untitled", "8-bit noise", 900, 500, 1);
run("Gaussian Blur...", "sigma=3");
makeLine(11, 222, 786, 226);
run("Plot Profile");
width = getWidth;
height = getHeight;
run("Copy");
selectWindow("Untitled");
makeRectangle(0,0,width,height);
run("Paste");

Hi Wayne, below is a workable chunk of code:

run("Bio-Formats Macro Extensions"); //loads Bio-Formats Macro Extensions for data import
Dialog.create("Choose the analysis you want to perform"); 
Dialog.addCheckbox("Homogeneity", false);
Dialog.addToSameRow();
Dialog.addNumber("How many Channels are in the image?", 0);
Dialog.show();

homo=Dialog.getCheckbox();
homoch=Dialog.getNumber();


if (homo==true) testimagesdir=getDirectory("Choose where Test_Images directory is"); // If at least one kind of analysis is to be done, you get a dialog box looking for the Test_Images directory 
else run("Close All");

resultsdir=testimagesdir + "ANALYSIS RESULTS/"; 
File.makeDirectory(resultsdir); // Create the folder ANALYSIS RESULTS, where results images will be stored  

list1 = getFileList(testimagesdir+"IMAGES\\");

for (i = 0; i <list1.length; i++){ 
Ext.openImagePlus(testimagesdir+"IMAGES\\" + list1[i]);
imagetitle=getInfo("image.filename");

if (homo==true) {if (matches(imagetitle, "(?i).*homo.*")){
	             run("Deinterleave", "how=&homoch");        	
                 homoimgheight=getHeight();
                 homoimgheiU=homoimgheight/4;
                 list2 = getList("image.titles");

                 for (a=0; a<list2.length; a++) {             
                 selectWindow(list2[a]);                
                 run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
                 run("Z Project...", "projection=[Average Intensity]");
                 makeLine(0, homoimgheight/2, homoimgheight, homoimgheight/2);
                 line1profile=getProfile();
                 run("Draw", "slice");
                 Plot.create("Profile line 1", "Pixels", "Gray value", line1profile);
                 Plot.setFrameSize(homoimgheight, 110);
                 Plot.setFontSize(12);
                 Plot.setAxisLabelSize(12.0, "plain");
                 Plot.setFormatFlags("11001100111111");
                 Plot.setStyle(0, "black,none,1.0,Line");
                 Plot.show();
                 selectWindow("AVG_"+list2[a]);
                 
                 makeLine(0, homoimgheiU, homoimgheight, homoimgheiU);
                 line2profile=getProfile();
                 run("Draw", "slice");
                 Plot.create("Profile line 2", "Pixels", "Gray values", line2profile);
                 Plot.setFrameSize(homoimgheight, 110);
                 Plot.setFontSize(12);
                 Plot.setAxisLabelSize(12.0, "plain");
                 Plot.setFormatFlags("11001100111111");
                 Plot.setStyle(0, "red,none,1.0,Line");
                 Plot.show();
                 selectWindow("AVG_"+list2[a]);

                 makeLine(0, homoimgheiU+homoimgheiU*2, homoimgheight, homoimgheiU+homoimgheiU*2);
                 line3profile=getProfile();
                 run("Draw", "slice");
                 Plot.create("Profile line 3", "Pixels", "Gray values", line3profile);
                 Plot.setFrameSize(homoimgheight, 110);
                 Plot.setFontSize(12);
                 Plot.setAxisLabelSize(12.0, "plain");
                 Plot.setFormatFlags("11001100111111");
                 Plot.setStyle(0, "blue,none,1.0,Line");
                 Plot.show();
                 selectWindow("AVG_"+list2[a]);    

                 newImage("Homogeneity Lines Profiles"+list2[a], "RGB white", 1120, 512, 1);
                 selectWindow("AVG_"+list2[a]); 
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);                                
                 makeRectangle(0, 0, 512, 512);
                 run("Paste");
                 selectWindow("Profile line 1");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);               
                 makeRectangle(515, 174, 595, 165);
                 run("Paste");               
                 close("Profile line 1");
                 selectWindow("Profile line 2");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);              
                 makeRectangle(515, 1, 595, 165);
                 run("Paste");
                 close("Profile line 2");
                 selectWindow("Profile line 3");
                 run("Copy");
                 selectWindow("Homogeneity Lines Profiles"+list2[a]);                 
                 makeRectangle(515, 348, 595, 165);
                 run("Paste");
                 close("Profile line 3");
                 
                 setColor(0, 0, 0);
                 drawLine(0, homoimgheight/2, homoimgheight, homoimgheight/2);
                 setColor(255, 0, 0);
                 drawLine(0, homoimgheiU, homoimgheight, homoimgheiU);
                 setColor(0, 0, 255);
                 drawLine(0, homoimgheiU+homoimgheiU*2, homoimgheight, homoimgheiU+homoimgheiU*2);
                 setColor(255,255,255);
                 setFont("SansSerif", 14);
                 drawString("Homogeneity_"+list2[a]+" Channel "+a+1,30,30);
                 saveAs("Jpeg", resultsdir+list2[a]+" Channel "+a+1);
                 close("AVG_"+list2[a]);
                 }
                 close("Homo*");
                 }
}
            

To run, it requires a stack called “homo” in a subfolder named “IMAGES” that is in a a folder called “Test_Images”.

Thanks for your help.

What we need is a Minimal working example (https://en.wikipedia.org/wiki/Minimal_working_example).

From the Wikipedia article:

“The important feature of a minimal working example is that it is as small and as simple as possible, such that it is just sufficient to demonstrate the problem, but without any additional complexity or dependencies which will make resolution harder.”

I posted this piece of code because it is the simplest working example already. It is a part of a larger script. As I wrote in the previous post, to run you need: a stack called “homo” in a subfolder named “IMAGES” that is in a folder called “Test_Images”. For simplicity, you could just say it’s 1 channel only.

As you could surely see, what it does is simply:
-open a stack called “homo”
-average the frames
-draw 3 lines at different positions in the averaged image
-plot the 3 line profiles with some cosmetic changes
-create a .jpeg image in which pasting the averaged image and the 3 line profile plots.
-color-code the 3 lines drawn in the averaged image
-save the .jpeg image

This code worked just fine until about a month ago. It’s about a couple of weeks that is behaving this weird and that’s why I am asking the community.
Thanks anyway.

Anyways, downgrading to Fiji 1.52 fixed the issue.

FYI

Hi @MIKEIII,
Create rectangles using the actual plot width and height and your macro should work as expected. For example:

   selectWindow("Profile line 1");
   pwidth = getWidth;
   pheight = getHeight;
   run("Copy");
   selectWindow("Homogeneity Lines Profiles"+list2[a]);               
   makeRectangle(515, 174, pwidth, pheight);

Is this what you’re looking to do?

newImage(“1”, “8-bit white”, 500, 500, 1);
newImage(“2”, “8-bit ramp”, 500, 500, 1);
setTool(“line”);
makeLine(134, 112, 288, 112);
//run(“Plot Profile”);
selectWindow(“2”);
run(“Line to Area”);
run(“Copy”);
selectWindow(“1”);
run(“Restore Selection”);
run(“Paste”);
run(“Select None”);
run(“Tile”);

@Wayne and @rondespain
Thank you very much Wayne. The getWidth and getHeight made it work in Fiji 1.53.

Do you have an idea on why with Fiji 1.52 was working fine without, whereas in 1.53 wasn’t?

Your macro failed because it assumes the size of the plot window is fixed but the size can now change based on the number font size or if a macro used something like Plot.setOptions(“addhspace=10 addvspace=5”) to make the margins larger.

ImageJ 1.53f is going to have a recordable Image.paste(x,y) macro function that will make it easier to specify the paste location.

Got you.
Thanks for the help!