Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Created September 11, 2019 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrueden/5318fe5239d2b6457e8b628aa7d1765a to your computer and use it in GitHub Desktop.
Save ctrueden/5318fe5239d2b6457e8b628aa7d1765a to your computer and use it in GitHub Desktop.
Create a vertical stack of images, each horizontally centered
#@ ImageJ ij
#@ File[] imageFiles
#@ File(style = "save") outFile
#@ boolean showStack
import java.util.Arrays
import net.imglib2.FinalInterval
import net.imglib2.type.numeric.integer.UnsignedByteType
import net.imglib2.util.Intervals
whiteValue = new UnsignedByteType(255)
centerHorizontal = { image, newWidth ->
dims = Intervals.dimensionsAsLongArray(image)
dims[0] = newWidth
extended = ij.op().transform().extendValueView(image, whiteValue)
return ij.op().transform().intervalView(extended, new FinalInterval(dims))
}
images = Arrays.stream(imageFiles).map{file -> ij.io().open(file.getAbsolutePath())}.collect()
maxWidth = images.stream().map{image -> image.dimension(0)}.collect().max()
centeredImages = images.stream().map{image -> centerHorizontal(image, maxWidth)}.collect()
stacked = ij.op().transform().concatenateView(centeredImages, 1)
if (showStack) ij.ui().show(stacked)
ij.io().save(stacked, outFile.getAbsolutePath())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment