Mercurial > repos > imgteam > split_image
comparison split.py @ 0:6b42bec75e69 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
| author | imgteam |
|---|---|
| date | Fri, 07 Mar 2025 08:08:05 +0000 |
| parents | |
| children | 73d7a4ffc03d |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:6b42bec75e69 |
|---|---|
| 1 import argparse | |
| 2 import math | |
| 3 | |
| 4 import giatools | |
| 5 import giatools.util | |
| 6 import numpy as np | |
| 7 import tifffile | |
| 8 | |
| 9 | |
| 10 parser = argparse.ArgumentParser() | |
| 11 parser.add_argument('input', type=str) | |
| 12 parser.add_argument('axis', type=str) | |
| 13 parser.add_argument('output', type=str) | |
| 14 parser.add_argument('--squeeze', action='store_true', default=False) | |
| 15 args = parser.parse_args() | |
| 16 | |
| 17 # Validate and normalize input parameters | |
| 18 assert len(args.axis) == 1, 'Axis input must be a single character.' | |
| 19 axis = args.axis.replace('S', 'C') | |
| 20 | |
| 21 # Read input image as TZYXC | |
| 22 img_in = giatools.Image.read(args.input) | |
| 23 | |
| 24 # Determine the axis to split along | |
| 25 axis_pos = img_in.axes.index(axis) | |
| 26 | |
| 27 # Perform the splitting | |
| 28 arr = np.moveaxis(img_in.data, axis_pos, 0) | |
| 29 decimals = math.ceil(math.log10(1 + arr.shape[0])) | |
| 30 output_filename_pattern = f'{args.output}/%0{decimals}d.tiff' | |
| 31 for img_idx, img in enumerate(arr): | |
| 32 img = np.moveaxis(img[None], 0, axis_pos) | |
| 33 | |
| 34 # Optionally, squeeze the image | |
| 35 if args.squeeze: | |
| 36 s = [axis_pos for axis_pos in range(len(img_in.axes)) if img.shape[axis_pos] == 1 and img_in.axes[axis_pos] not in 'YX'] | |
| 37 img = np.squeeze(img, axis=tuple(s)) | |
| 38 img_axes = giatools.util.str_without_positions(img_in.axes, s) | |
| 39 else: | |
| 40 img_axes = img_in.axes | |
| 41 | |
| 42 # Save the result | |
| 43 filename = output_filename_pattern % (img_idx + 1) | |
| 44 tifffile.imwrite(filename, img, metadata=dict(axes=img_axes)) |
