Source code

Download source archive ↓

upstream/tools/image_processing/imagej2/imagej2_crop_jython_script.py

1import sys
2
3from ij import IJ
4from ij.plugin import Duplicator
5
6# Fiji Jython interpreter implements Python 2.5 which does not
7# provide support for argparse.
8input_file = sys.argv[-13]
9xleft = int(sys.argv[-12])
10width = int(sys.argv[-11])
11ytop = int(sys.argv[-10])
12height = int(sys.argv[-9])
13first_channel = int(sys.argv[-8])
14last_channel = int(sys.argv[-7])
15first_slice = int(sys.argv[-6])
16last_slice = int(sys.argv[-5])
17first_frame = int(sys.argv[-4])
18last_frame = int(sys.argv[-3])
19output_filename = sys.argv[-2]
20output_datatype = sys.argv[-1]
21
22# Open the input image file.
23input_image_plus = IJ.openImage(input_file)
24
25# Get image dimensions (width, height, nChannels, nSlices, nFrames)
26image_dims = input_image_plus.getDimensions()
27
28# Create a copy of the image.
29input_image_plus_copy = input_image_plus.duplicate()
30
31# Determine if crop in XY is needed:
32if xleft != 0 or width != 0 or ytop != 0 or height != 0:
33    # Need to define a ROI
34    if width == 0:
35        width = image_dims[0] - xleft
36    if height == 0:
37        height = image_dims[1] - ytop
38    input_image_plus_copy.setRoi(xleft, ytop, width, height)
39
40# Replace 0's with default:
41if last_channel == 0:
42    last_channel = image_dims[2]
43if last_slice == 0:
44    last_slice = image_dims[3]
45if last_frame == 0:
46    last_frame = image_dims[4]
47print("Original dimensions:")
48print(image_dims)
49# This will also crop in XY is a ROI has been set
50input_image_plus_copy = Duplicator().run(input_image_plus_copy, first_channel, last_channel, first_slice, last_slice, first_frame, last_frame)
51print("Final dimensions:")
52print(input_image_plus_copy.getDimensions())
53
54# Save the ImagePlus object as a new image.
55IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)