comparison concat_channels.py @ 3:4c43875c790c draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:25:29 +0000
parents 212627bfb759
children 2592a29a785e
comparison
equal deleted inserted replaced
2:212627bfb759 3:4c43875c790c
1 import argparse 1 import argparse
2 import warnings
3 2
4 import numpy as np 3 import numpy as np
5 import skimage.io 4 import skimage.io
6 import skimage.util 5 import skimage.util
7 6
8 7
9 def concat_channels(input_image_paths, output_image_path, axis): 8 def concat_channels(input_image_paths, output_image_path, axis, preserve_values):
10 images = [] 9 images = []
11 for image_path in input_image_paths: 10 for image_path in input_image_paths:
11
12 raw_image = skimage.io.imread(image_path) 12 raw_image = skimage.io.imread(image_path)
13 if len(raw_image.shape) == 2: 13 if len(raw_image.shape) == 2:
14 if axis == 0: 14 if axis == 0:
15 raw_image = [raw_image] 15 raw_image = [raw_image]
16 else: 16 else:
17 raw_image = np.expand_dims(raw_image, 2) 17 raw_image = np.expand_dims(raw_image, 2)
18
19 # Preserve values: Convert to `float` dtype without changing the values
20 if preserve_values:
21 raw_image = raw_image.astype(float)
22
23 # Preserve brightness: Scale values to 0..1
24 else:
25 raw_image = skimage.util.img_as_float(raw_image)
26
18 images.append(raw_image) 27 images.append(raw_image)
28
29 # Do the concatenation and save
19 res = np.concatenate(images, axis) 30 res = np.concatenate(images, axis)
20 with warnings.catch_warnings(): 31 skimage.io.imsave(output_image_path, res, plugin='tifffile')
21 warnings.simplefilter("ignore")
22 res = skimage.util.img_as_uint(res) # Attention: precision loss
23 skimage.io.imsave(output_image_path, res, plugin='tifffile')
24 32
25 33
26 if __name__ == "__main__": 34 if __name__ == "__main__":
27 parser = argparse.ArgumentParser() 35 parser = argparse.ArgumentParser()
28 parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+', help='input file') 36 parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+')
29 parser.add_argument('-o', dest='out_file', type=argparse.FileType('w'), help='out file (TIFF)') 37 parser.add_argument('-o', dest='out_file', type=argparse.FileType('w'))
30 parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0, 2], help='concatenation axis') 38 parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0, 2])
39 parser.add_argument('--preserve_values', default=False, action='store_true')
31 args = parser.parse_args() 40 args = parser.parse_args()
32 41
33 concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis) 42 concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis, args.preserve_values)