Mercurial > repos > imgteam > split_image
comparison split.py @ 1:4b7940d0c051 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
| author | imgteam |
|---|---|
| date | Sat, 12 Apr 2025 15:05:17 +0000 |
| parents | 5deaa390ca62 |
| children |
comparison
equal
deleted
inserted
replaced
| 0:5deaa390ca62 | 1:4b7940d0c051 |
|---|---|
| 16 | 16 |
| 17 # Validate and normalize input parameters | 17 # Validate and normalize input parameters |
| 18 assert len(args.axis) == 1, 'Axis input must be a single character.' | 18 assert len(args.axis) == 1, 'Axis input must be a single character.' |
| 19 axis = args.axis.replace('S', 'C') | 19 axis = args.axis.replace('S', 'C') |
| 20 | 20 |
| 21 # Read input image as TZYXC | 21 # Read input image with normalized axes |
| 22 img_in = giatools.Image.read(args.input) | 22 img_in = giatools.Image.read(args.input) |
| 23 | 23 |
| 24 # Determine the axis to split along | 24 # Determine the axis to split along |
| 25 axis_pos = img_in.axes.index(axis) | 25 axis_pos = img_in.axes.index(axis) |
| 26 | 26 |
| 29 decimals = math.ceil(math.log10(1 + arr.shape[0])) | 29 decimals = math.ceil(math.log10(1 + arr.shape[0])) |
| 30 output_filename_pattern = f'{args.output}/%0{decimals}d.tiff' | 30 output_filename_pattern = f'{args.output}/%0{decimals}d.tiff' |
| 31 for img_idx, img in enumerate(arr): | 31 for img_idx, img in enumerate(arr): |
| 32 img = np.moveaxis(img[None], 0, axis_pos) | 32 img = np.moveaxis(img[None], 0, axis_pos) |
| 33 | 33 |
| 34 # Construct the output image, remove axes added by normalization | |
| 35 img_out = giatools.Image( | |
| 36 data=img, | |
| 37 axes=img_in.axes, | |
| 38 ).squeeze_like( | |
| 39 img_in.original_axes, | |
| 40 ) | |
| 41 | |
| 34 # Optionally, squeeze the image | 42 # Optionally, squeeze the image |
| 35 if args.squeeze: | 43 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'] | 44 s = [ |
| 37 img = np.squeeze(img, axis=tuple(s)) | 45 axis_pos for axis_pos in range(len(img_out.axes)) |
| 38 img_axes = giatools.util.str_without_positions(img_in.axes, s) | 46 if img_out.data.shape[axis_pos] == 1 and img_out.axes[axis_pos] not in 'YX' |
| 39 else: | 47 ] |
| 40 img_axes = img_in.axes | 48 img_out = img_out.squeeze_like( |
| 49 giatools.util.str_without_positions(img_out.axes, s), | |
| 50 ) | |
| 41 | 51 |
| 42 # Save the result | 52 # Save the result |
| 43 filename = output_filename_pattern % (img_idx + 1) | 53 filename = output_filename_pattern % (img_idx + 1) |
| 44 tifffile.imwrite(filename, img, metadata=dict(axes=img_axes)) | 54 tifffile.imwrite(filename, img_out.data, metadata=dict(axes=img_out.axes)) |
