annotate imagej2_crop_jython_script.py @ 3:f7ae316b00e4 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
author imgteam
date Wed, 25 Sep 2024 16:02:30 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
1 import sys
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
2
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
3 from ij import IJ
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
4 from ij.plugin import Duplicator
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
5
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
6 # Fiji Jython interpreter implements Python 2.5 which does not
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
7 # provide support for argparse.
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
8 input_file = sys.argv[-13]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
9 xleft = int(sys.argv[-12])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
10 width = int(sys.argv[-11])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
11 ytop = int(sys.argv[-10])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
12 height = int(sys.argv[-9])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
13 first_channel = int(sys.argv[-8])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
14 last_channel = int(sys.argv[-7])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
15 first_slice = int(sys.argv[-6])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
16 last_slice = int(sys.argv[-5])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
17 first_frame = int(sys.argv[-4])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
18 last_frame = int(sys.argv[-3])
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
19 output_filename = sys.argv[-2]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
20 output_datatype = sys.argv[-1]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
21
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
22 # Open the input image file.
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
23 input_image_plus = IJ.openImage(input_file)
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
24
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
25 # Get image dimensions (width, height, nChannels, nSlices, nFrames)
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
26 image_dims = input_image_plus.getDimensions()
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
27
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
28 # Create a copy of the image.
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
29 input_image_plus_copy = input_image_plus.duplicate()
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
30
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
31 # Determine if crop in XY is needed:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
32 if xleft != 0 or width != 0 or ytop != 0 or height != 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
33 # Need to define a ROI
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
34 if width == 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
35 width = image_dims[0] - xleft
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
36 if height == 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
37 height = image_dims[1] - ytop
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
38 input_image_plus_copy.setRoi(xleft, ytop, width, height)
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
39
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
40 # Replace 0's with default:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
41 if last_channel == 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
42 last_channel = image_dims[2]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
43 if last_slice == 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
44 last_slice = image_dims[3]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
45 if last_frame == 0:
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
46 last_frame = image_dims[4]
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
47 print("Original dimensions:")
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
48 print(image_dims)
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
49 # This will also crop in XY is a ROI has been set
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
50 input_image_plus_copy = Duplicator().run(input_image_plus_copy, first_channel, last_channel, first_slice, last_slice, first_frame, last_frame)
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
51 print("Final dimensions:")
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
52 print(input_image_plus_copy.getDimensions())
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
53
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
54 # Save the ImagePlus object as a new image.
f7ae316b00e4 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents:
diff changeset
55 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)