Mercurial > repos > imgteam > concat_channels
comparison concat_channels.py @ 5:8d50a0a9e4af draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
| author | imgteam |
|---|---|
| date | Sun, 07 Dec 2025 16:16:03 +0000 |
| parents | 2592a29a785e |
| children | 999c5941a6f0 |
comparison
equal
deleted
inserted
replaced
| 4:2592a29a785e | 5:8d50a0a9e4af |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 | 2 |
| 3 import giatools.io | 3 import giatools |
| 4 import numpy as np | 4 import numpy as np |
| 5 import skimage.io | 5 import skimage.io |
| 6 import skimage.util | 6 import skimage.util |
| 7 | 7 |
| 8 | 8 |
| 9 def concat_channels(input_image_paths, output_image_path, axis, preserve_values): | 9 normalized_axes = 'QTZYXC' |
| 10 | |
| 11 | |
| 12 def concat_channels( | |
| 13 input_image_paths: list[str], | |
| 14 output_image_path: str, | |
| 15 axis: str, | |
| 16 preserve_values: bool, | |
| 17 ): | |
| 18 # Create list of arrays to be concatenated | |
| 10 images = [] | 19 images = [] |
| 11 for image_path in input_image_paths: | 20 for image_path in input_image_paths: |
| 12 | 21 |
| 13 raw_image = giatools.io.imread(image_path) | 22 img = giatools.Image.read(image_path, normalize_axes=normalized_axes) |
| 14 if len(raw_image.shape) == 2: | 23 arr = img.data |
| 15 if axis == 0: | |
| 16 raw_image = [raw_image] | |
| 17 else: | |
| 18 raw_image = np.expand_dims(raw_image, 2) | |
| 19 | 24 |
| 20 # Preserve values: Convert to `float` dtype without changing the values | 25 # Preserve values: Convert to `float` dtype without changing the values |
| 21 if preserve_values: | 26 if preserve_values: |
| 22 raw_image = raw_image.astype(float) | 27 arr = arr.astype(float) |
| 23 | 28 |
| 24 # Preserve brightness: Scale values to 0..1 | 29 # Preserve brightness: Scale values to 0..1 |
| 25 else: | 30 else: |
| 26 raw_image = skimage.util.img_as_float(raw_image) | 31 arr = skimage.util.img_as_float(arr) |
| 27 | 32 |
| 28 images.append(raw_image) | 33 images.append(arr) |
| 29 | 34 |
| 30 # Do the concatenation and save | 35 # Do the concatenation |
| 31 res = np.concatenate(images, axis) | 36 axis_pos = normalized_axes.index(axis) |
| 32 skimage.io.imsave(output_image_path, res, plugin='tifffile') | 37 arr = np.concatenate(images, axis_pos) |
| 38 res = giatools.Image(arr, normalized_axes) | |
| 39 | |
| 40 # Squeeze singleton axes and save | |
| 41 squeezed_axes = ''.join(np.array(list(res.axes))[np.array(arr.shape) > 1]) | |
| 42 res = res.squeeze_like(squeezed_axes) | |
| 43 res.write(output_image_path, backend='tifffile') | |
| 33 | 44 |
| 34 | 45 |
| 35 if __name__ == "__main__": | 46 if __name__ == "__main__": |
| 36 parser = argparse.ArgumentParser() | 47 parser = argparse.ArgumentParser() |
| 37 parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+') | 48 parser.add_argument('input_files', type=str, nargs='+') |
| 38 parser.add_argument('-o', dest='out_file', type=argparse.FileType('w')) | 49 parser.add_argument('out_file', type=str) |
| 39 parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0, 2]) | 50 parser.add_argument('axis', type=str) |
| 40 parser.add_argument('--preserve_values', default=False, action='store_true') | 51 parser.add_argument('--preserve_values', default=False, action='store_true') |
| 41 args = parser.parse_args() | 52 args = parser.parse_args() |
| 42 | 53 |
| 43 concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis, args.preserve_values) | 54 concat_channels( |
| 55 args.input_files, | |
| 56 args.out_file, | |
| 57 args.axis, | |
| 58 args.preserve_values, | |
| 59 ) |
