Mercurial > repos > imgteam > concat_channels
diff 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 |
line wrap: on
line diff
--- a/concat_channels.py Mon Jul 22 04:56:23 2019 -0400 +++ b/concat_channels.py Sun Dec 07 16:15:54 2025 +0000 @@ -1,33 +1,59 @@ import argparse -import sys -import warnings + +import giatools import numpy as np import skimage.io import skimage.util -def concat_channels(input_image_paths, output_image_path, axis): + +normalized_axes = 'QTZYXC' + + +def concat_channels( + input_image_paths: list[str], + output_image_path: str, + axis: str, + preserve_values: bool, +): + # Create list of arrays to be concatenated images = [] for image_path in input_image_paths: - raw_image = skimage.io.imread(image_path) - if len(raw_image.shape) == 2: - if axis == 0: - raw_image = [raw_image] - else: - raw_image = np.expand_dims(raw_image, 2) - images.append(raw_image) - res = np.concatenate(images, axis) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - res = skimage.util.img_as_uint(res) #Attention: precision loss - skimage.io.imsave(output_image_path, res, plugin='tifffile') + + img = giatools.Image.read(image_path, normalize_axes=normalized_axes) + arr = img.data + + # Preserve values: Convert to `float` dtype without changing the values + if preserve_values: + arr = arr.astype(float) + + # Preserve brightness: Scale values to 0..1 + else: + arr = skimage.util.img_as_float(arr) + + images.append(arr) + + # Do the concatenation + axis_pos = normalized_axes.index(axis) + arr = np.concatenate(images, axis_pos) + res = giatools.Image(arr, normalized_axes) + + # Squeeze singleton axes and save + squeezed_axes = ''.join(np.array(list(res.axes))[np.array(arr.shape) > 1]) + res = res.squeeze_like(squeezed_axes) + res.write(output_image_path, backend='tifffile') + if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+', help='input file') - parser.add_argument('-o', dest='out_file', type=argparse.FileType('w'), help='out file (TIFF)') - parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0,2], help='concatenation axis') + parser.add_argument('input_files', type=str, nargs='+') + parser.add_argument('out_file', type=str) + parser.add_argument('axis', type=str) + parser.add_argument('--preserve_values', default=False, action='store_true') args = parser.parse_args() - # print([x.name for x in args.input_files], args.out_file.name, args.axis) - concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis) - # concat_channels(args.input_files, args.out_file, args.axis) + concat_channels( + args.input_files, + args.out_file, + args.axis, + args.preserve_values, + )
