Mercurial > repos > bgruening > imagej2_crop
annotate imagej2_enhance_contrast_jython_script.py @ 0:018144807556 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
author | bgruening |
---|---|
date | Tue, 24 Sep 2024 17:12:52 +0000 |
parents | |
children |
rev | line source |
---|---|
0
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
1 import sys |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
2 |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
3 from ij import IJ |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
4 |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
5 # Fiji Jython interpreter implements Python 2.5 which does not |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
6 # provide support for argparse. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
7 input = sys.argv[-6] |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
8 equalize_histogram = sys.argv[-5] == "yes" |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
9 saturated_pixels = sys.argv[-4] |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
10 normalize = sys.argv[-3] == "yes" |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
11 tmp_output_path = sys.argv[-2] |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
12 output_datatype = sys.argv[-1] |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
13 |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
14 # Open the input image file. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
15 input_image_plus = IJ.openImage(input) |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
16 |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
17 # Create a copy of the image. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
18 input_image_plus_copy = input_image_plus.duplicate() |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
19 image_processor_copy = input_image_plus_copy.getProcessor() |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
20 bit_depth = image_processor_copy.getBitDepth() |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
21 |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
22 # Set the options |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
23 options = [] |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
24 # If equalize_histogram, saturated_pixels and normalize are ignored. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
25 if equalize_histogram: |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
26 options.append("equalize") |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
27 else: |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
28 if saturated_pixels not in [None, "None"]: |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
29 # Fiji allows only a single decimal place for this value. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
30 options.append("saturated=%.3f" % float(saturated_pixels)) |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
31 # Normalization of RGB images is not supported. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
32 if bit_depth != 24 and normalize: |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
33 options.append("normalize") |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
34 # Run the command. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
35 options = "%s" % " ".join(options) |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
36 IJ.run(input_image_plus_copy, "Enhance Contrast...", options) |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
37 # Save the ImagePlus object as a new image. |
018144807556
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
bgruening
parents:
diff
changeset
|
38 IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path) |