Mercurial > repos > imgteam > concat_channels
comparison concat_channels.py @ 2:ad1caf2331c6 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:15:54 +0000 |
| parents | 31c8c700d98d |
| children | 01c1d5af33be |
comparison
equal
deleted
inserted
replaced
| 1:31c8c700d98d | 2:ad1caf2331c6 |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 import sys | 2 |
| 3 import warnings | 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 def concat_channels(input_image_paths, output_image_path, axis): | 8 |
| 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 | |
| 9 images = [] | 19 images = [] |
| 10 for image_path in input_image_paths: | 20 for image_path in input_image_paths: |
| 11 raw_image = skimage.io.imread(image_path) | 21 |
| 12 if len(raw_image.shape) == 2: | 22 img = giatools.Image.read(image_path, normalize_axes=normalized_axes) |
| 13 if axis == 0: | 23 arr = img.data |
| 14 raw_image = [raw_image] | 24 |
| 15 else: | 25 # Preserve values: Convert to `float` dtype without changing the values |
| 16 raw_image = np.expand_dims(raw_image, 2) | 26 if preserve_values: |
| 17 images.append(raw_image) | 27 arr = arr.astype(float) |
| 18 res = np.concatenate(images, axis) | 28 |
| 19 with warnings.catch_warnings(): | 29 # Preserve brightness: Scale values to 0..1 |
| 20 warnings.simplefilter("ignore") | 30 else: |
| 21 res = skimage.util.img_as_uint(res) #Attention: precision loss | 31 arr = skimage.util.img_as_float(arr) |
| 22 skimage.io.imsave(output_image_path, res, plugin='tifffile') | 32 |
| 33 images.append(arr) | |
| 34 | |
| 35 # Do the concatenation | |
| 36 axis_pos = normalized_axes.index(axis) | |
| 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') | |
| 44 | |
| 23 | 45 |
| 24 if __name__ == "__main__": | 46 if __name__ == "__main__": |
| 25 parser = argparse.ArgumentParser() | 47 parser = argparse.ArgumentParser() |
| 26 parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+', help='input file') | 48 parser.add_argument('input_files', type=str, nargs='+') |
| 27 parser.add_argument('-o', dest='out_file', type=argparse.FileType('w'), help='out file (TIFF)') | 49 parser.add_argument('out_file', type=str) |
| 28 parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0,2], help='concatenation axis') | 50 parser.add_argument('axis', type=str) |
| 51 parser.add_argument('--preserve_values', default=False, action='store_true') | |
| 29 args = parser.parse_args() | 52 args = parser.parse_args() |
| 30 | 53 |
| 31 # print([x.name for x in args.input_files], args.out_file.name, args.axis) | 54 concat_channels( |
| 32 concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis) | 55 args.input_files, |
| 33 # concat_channels(args.input_files, args.out_file, args.axis) | 56 args.out_file, |
| 57 args.axis, | |
| 58 args.preserve_values, | |
| 59 ) |
