Mercurial > repos > imgteam > imagej2_crop
comparison imagej2_math_jython_script.py @ 0:db3066ae9b09 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:28:45 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:db3066ae9b09 |
|---|---|
| 1 import sys | |
| 2 | |
| 3 from ij import IJ | |
| 4 | |
| 5 # Fiji Jython interpreter implements Python 2.5 which does not | |
| 6 # provide support for argparse.\ | |
| 7 input_file = sys.argv[-7] | |
| 8 operation = sys.argv[-6] | |
| 9 expression = sys.argv[-5] | |
| 10 if sys.argv[-4] in [None, "None"]: | |
| 11 bin_constant = None | |
| 12 else: | |
| 13 bin_constant = int(sys.argv[-4]) | |
| 14 if sys.argv[-3] in [None, "None"]: | |
| 15 float_constant = None | |
| 16 else: | |
| 17 float_constant = float(sys.argv[-3]) | |
| 18 tmp_output_path = sys.argv[-2] | |
| 19 output_datatype = sys.argv[-1] | |
| 20 | |
| 21 print("\ninput_file: %s\n" % str(input_file)) | |
| 22 print("\noperation: %s\n" % str(operation)) | |
| 23 print("\nexpression: %s\n" % str(expression)) | |
| 24 print("\nbin_constant: %s\n" % str(bin_constant)) | |
| 25 print("\nfloat_constant: %s\n" % str(float_constant)) | |
| 26 print("\ntmp_output_path: %s\n" % str(tmp_output_path)) | |
| 27 print("\noutput_datatype: %s\n" % str(output_datatype)) | |
| 28 | |
| 29 # Open the input image file. | |
| 30 input_image_plus = IJ.openImage(input_file) | |
| 31 | |
| 32 # Create a copy of the image. | |
| 33 input_image_plus_copy = input_image_plus.duplicate() | |
| 34 image_processor_copy = input_image_plus_copy.getProcessor() | |
| 35 bit_depth = image_processor_copy.getBitDepth() | |
| 36 | |
| 37 if operation.find("_") > 0: | |
| 38 # Square_Root. | |
| 39 new_operation = operation.replace("_", " ") | |
| 40 elif operation in ["Square", "Log", "Exp", "Abs", "Reciprocal"]: | |
| 41 # Unfortunately some ImageJ commands require a "..." ending | |
| 42 # while others do not. There seems to be no pattern. | |
| 43 new_operation = "%s" % operation | |
| 44 else: | |
| 45 new_operation = "%s..." % operation | |
| 46 | |
| 47 if operation == "Macro": | |
| 48 # Apply the macro code to the image via a call to it's | |
| 49 # ImageProcessor since this option does not work using | |
| 50 # the IJ.run() method. | |
| 51 new_expression = expression.lstrip('"').rstrip('"') | |
| 52 options = "code=%s" % new_expression | |
| 53 image_processor_copy.applyMacro(new_expression) | |
| 54 elif operation == "Min": | |
| 55 # Min does not work without using the ImageProcessor. | |
| 56 image_processor_copy.min(float_constant) | |
| 57 elif operation == "Max": | |
| 58 # Max does not work without using the ImageProcessor. | |
| 59 image_processor_copy.max(float_constant) | |
| 60 elif operation == "Abs": | |
| 61 if bit_depth not in [16, 32]: | |
| 62 # Convert the image to 32-bit. | |
| 63 IJ.run(input_image_plus_copy, "32-bit", "") | |
| 64 IJ.run(input_image_plus_copy, new_operation, "") | |
| 65 elif operation == "Reciprocal": | |
| 66 if bit_depth != 32: | |
| 67 # Convert the image to 32 bit. | |
| 68 IJ.run(input_image_plus_copy, "32-bit", "") | |
| 69 IJ.run(input_image_plus_copy, new_operation, "") | |
| 70 else: | |
| 71 if operation in ["AND", "OR", "XOR"]: | |
| 72 # Value is a binary number. | |
| 73 options = "value=%d" % bin_constant | |
| 74 elif operation in ["Log", "Exp", "Square", "Square_Root"]: | |
| 75 # No constant value. | |
| 76 options = "" | |
| 77 else: | |
| 78 # Value is a floating point number. | |
| 79 options = "value=%.3f" % float_constant | |
| 80 IJ.run(input_image_plus_copy, "%s" % new_operation, "%s" % options) | |
| 81 # Save the ImagePlus object as a new image. | |
| 82 IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path) |
