Mercurial > repos > imgteam > imagej2_watershed_binary
annotate imagej2_crop_jython_script.py @ 3:29aca8eebdaa 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:00:05 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 3 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 1 import sys | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 2 | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 5 | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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. | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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]) | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 21 | 
| 
29aca8eebdaa
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. | 
| 
29aca8eebdaa
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) | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 24 | 
| 
29aca8eebdaa
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) | 
| 
29aca8eebdaa
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() | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 27 | 
| 
29aca8eebdaa
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. | 
| 
29aca8eebdaa
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() | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 30 | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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) | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 39 | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
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: | 
| 
29aca8eebdaa
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] | 
| 
29aca8eebdaa
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:") | 
| 
29aca8eebdaa
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) | 
| 
29aca8eebdaa
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 | 
| 
29aca8eebdaa
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) | 
| 
29aca8eebdaa
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:") | 
| 
29aca8eebdaa
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()) | 
| 
29aca8eebdaa
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
 imgteam parents: diff
changeset | 53 | 
| 
29aca8eebdaa
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. | 
| 
29aca8eebdaa
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) | 
