ImageJ Macro equivalent of run("Copy") and run("Paste")

I’m looking for a way to copy-paste an ROI content with a command instead of run(“Copy”) and run(“Paste”). Very basic question but I can’t find the answer.

Thank you,
Aleks

Hi @Aleksandar,

When you say “ROI content”, do you mean pixel data? Do you just want to duplicate an image region? In which case all you need is

run("Duplicate...");

D.

Hi @djpbarry,
Yes, I would like to copy the pixel data in one part of the image and copy it to another part of the image. I’d like to make this compatible with batch processing without it all running on the screen.

Thanks,
Aleks

The following JavaScript code, created using the recorder (Plugins>Macros>Record), copies the pixel data from one part of an image and pastes it into another part. Nothing is displayed if you remove “img.show()”.

  img = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
  img.setRoi(150,190,95,175);
  IJ.run(img, "Copy", "");
  img.setRoi(50,30,95,175);
  IJ.run(img, "Paste", "");
  IJ.run(img, "Select None", "");
  img.show();

Here is a version that avoids IJ.run() calls:

  img = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
  img.setRoi(150,190,95,175);
  img.copy();
  img.setRoi(50,30,95,175);
  img.paste();
  img.deleteRoi();
  img.show();

Thanks for the advice. Shifting to the JavaScript code does what I asked for and I can that in one case.

In another case, I need to remain in the .ijm. I would like the following code executed on an already OPENED stack WITHOUT displaying the changes until the loop is completed. Or something else that may speed it up.

setBatchMode(true);
setColor(0,0,0);
	for(i=1;i<=Zsections;i++)
		{
			setSlice(i);
			makeRectangle(1, 1, width, height);
			run("Copy");
			fill();
			makeRectangle(FINALchangesX[i], FINALchangesY[i], width, height);
			run("Paste");
		}
setBatchMode(false);

Thanks in advance

You can hide the stack using setBatchMode(“hide”) and show it again when the loop finishes, as in this example:

  run("T1 Head (2.4M, 16-bits)");
  setBatchMode("hide")
  setColor(0,0,0);
  for (i=1;i<=nSlices; i++) {
     setSlice(i);
     makeRectangle(1, 1, getWidth, getHeight);
     run("Copy");
     fill();
     makeRectangle(i, i, getWidth, getHeight);
     run("Paste");
  }
  run("Select None");
  setBatchMode(false);
  doCommand("Start Animation");

Thank you. setBatchMode(“hide”) solved the issue also