from ij import IJ,ImagePlus,ImageStack,WindowManager,CompositeImage

imp=WindowManager.getCurrentImage()
print(imp.getTitle())
width=1360
height=1024
twidth=imp.getWidth()
theight=imp.getHeight()
nx=int(twidth/width)
ny=int(theight/height)
print([twidth,theight])
stack=ImageStack(width,height)
tstack=imp.getStack()
nslices=tstack.getSize()
print([nx,ny,nslices])
nt=nx*ny
for i in range(nx):
	xpos=i*width
	if((xpos+width)>=twidth):
		xpos=twidth-width-1
	for j in range(ny):
		ypos=j*height
		if((ypos+height)>=theight):
			ypos=theight-height-1
		cropped=tstack.crop(xpos,ypos,0,width,height,nslices)
		tsize=cropped.getSize()
		#print([xpos,ypos,tsize])
		for k in range(tsize):
			stack.addSlice(cropped.getProcessor(k+1))

timp=ImagePlus("tiled",stack)
olddims=imp.getDimensions()
timp.setDimensions(olddims[2],olddims[3],nt)
timp.setOpenAsHyperStack(True)
CompositeImage(timp,0).show()
