Login  Register

Re: Splitting single image in four

Posted by Swayne, Theresa C. on Jul 01, 2016; 5:02pm
URL: http://imagej.273.s1.nabble.com/Splitting-single-image-in-four-tp5016797p5016799.html

Hello and welcome!

There was a recent thread on the forum (http://forum.imagej.net/t/macro-for-automatic-saving-and-filename-designation/1966/7?u=tswayne)
 that included a macro that does essentially what you’re asking.

Below, I’ve included the macro that resulted from the forum discussion. This is by Thomas E. Sharp (Tgoose on the forum) with some additions by me.

To use the macro, copy the text, then open the script editor (in Fiji, File > New > Script), and paste the text in.
Under the Language menu choose IJ1 Macro. Save the file with the .ijm extension.
To run it, just open the image you want to crop and then click Run at the bottom of the script window.

More about macros in the User Guide: http://imagej.net/docs/guide/146-14.html#toc-Section-14

Hope this helps.

===================
n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
id = getImageID();
title = getTitle();
dotIndex = indexOf(title, ".");
basename = substring(title, 0, dotIndex);
path = getDirectory("image");
getLocationAndSize(locX, locY, sizeW, sizeH);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
for (y = 0; y < n; y++) {
  offsetY = y * height / n;
  for (x = 0; x < n; x++) {
    offsetX = x * width / n;
    selectImage(id);
    call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
    tileTitle = basename + "[" + x + "," + y + "].tif";
    // using the ampersand allows spaces in the tileTitle to be handled correctly
    run("Duplicate...", "title=&tileTitle");
    makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
    run("Crop");
    selectWindow(tileTitle);
    saveAs("tiff",path+tileTitle);
    }
  }
selectImage(id);
close();

==================

> On Jul 1, 2016, at 11:53 AM, akp19 <[hidden email]> wrote:
>
> Hi!
> I am quite new to ImageJ and not able to perform a relative simple task. I
> need to split a single image into 4 sub-images. Eg. a 1000*1000 image into
> four 250*250 images. How do I go about this? Will appreciate any help! Thank
> you!
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Splitting-single-image-in-four-tp5016797.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

------------------------------------
Theresa Swayne, Ph.D.
Manager
Confocal and Specialized Microscopy Shared Resource

Herbert Irving Comprehensive Cancer Center
Columbia University Medical Center
1130 St. Nicholas Ave., Room 222A
New York, NY 10032
Phone: 212-851-4613
[hidden email]


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html