# HG changeset patch
# User imgteam
# Date 1775337727 0
# Node ID d45a07063da15e44d2f48ce57cb003768be854e1
# Parent 227e8928af6e54d43f0dbea8b696976e27cc9d08
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit df96ae15da34285b0a9d435a48924665fff37d6a
diff -r 227e8928af6e -r d45a07063da1 split.py
--- a/split.py Fri Dec 12 21:02:25 2025 +0000
+++ b/split.py Sat Apr 04 21:22:07 2026 +0000
@@ -1,33 +1,45 @@
import argparse
import math
-import os
import pathlib
-import shutil
import giatools
+import giatools.io
import numpy as np
-import tifffile
class OutputWriter:
- def __init__(self, dir_path: pathlib.Path, num_images: int, squeeze: bool, verbose: bool):
- print(f'Writing {num_images} image(s)')
+ def __init__(
+ self,
+ dir_path: pathlib.Path,
+ num_images: int,
+ squeeze: bool,
+ verbose: bool,
+ offset: int = 0,
+ step: int = 1,
+ count: int | None = None,
+ ):
+ self.positions = np.arange(num_images)[offset::step] + 1
+ if count is not None:
+ self.positions = self.positions[:count]
+
+ print(f'Writing {len(self.positions)} out of {num_images} image(s)')
decimals = math.ceil(math.log10(1 + num_images))
self.output_filepath_pattern = str(dir_path / f'%0{decimals}d.tiff')
- self.last_idx = 0
+ self.last_pos = 0
self.squeeze = squeeze
self.verbose = verbose
def write(self, img: giatools.Image):
- self.last_idx += 1
- if self.squeeze:
- img = img.squeeze()
- if self.last_idx == 1 or self.verbose:
- prefix = f'Output {self.last_idx}' if self.verbose else 'Output'
- print(f'{prefix} axes:', img.axes)
- print(f'{prefix} shape:', img.data.shape)
- img.write(self.output_filepath_pattern % self.last_idx)
+ self.last_pos += 1
+ if self.last_pos in self.positions:
+ if self.squeeze:
+ img = img.squeeze()
+ if self.last_pos == self.positions[0] or self.verbose:
+ prefix = f'Output {self.last_pos}' if self.verbose else 'Output'
+ print(f'{prefix} axes:', img.axes)
+ print(f'{prefix} shape:', img.data.shape)
+ img.write(self.output_filepath_pattern % self.last_pos)
if __name__ == '__main__':
@@ -37,52 +49,31 @@
parser.add_argument('axis', type=str, choices=list(giatools.default_normalized_axes) + ['S', ''])
parser.add_argument('output', type=pathlib.Path)
parser.add_argument('--squeeze', action='store_true', default=False)
+ parser.add_argument('offset', type=int)
+ parser.add_argument('step', type=int)
+ parser.add_argument('--count', type=int)
args = parser.parse_args()
# If splitting a file that contains multiple images...
if args.axis == '':
- # Peek the number of series in the input file (if it is a TIFF)
- try:
- with tifffile.TiffFile(args.input) as tiff:
- num_tiff_series = len(tiff.series)
- print(f'Found TIFF with {num_tiff_series} series')
- except tifffile.TiffFileError:
- num_tiff_series = 0 # not a TIFF file
- print('Not a TIFF file')
+ # Peek the number of images
+ num_images = giatools.io.peek_num_images_in_file(args.input)
+ print(f'Found {num_images} image(s) in file')
- # If the file is a multi-series TIFF, extract the individual series
- # (for consistency, also accept only a single series if squeezing is requested)
- if num_tiff_series >= 2 or (num_tiff_series == 1 and args.squeeze):
- output = OutputWriter(
- dir_path=args.output,
- num_images=num_tiff_series,
- squeeze=args.squeeze,
- verbose=True,
- )
- for series in range(num_tiff_series):
- img = giatools.Image.read(args.input, series=series)
- output.write(
- img.squeeze_like(img.original_axes),
- )
-
- # Otherwise, there is nothing to be split (or squeeze)
- # (the input is either a single-series TIFF or not a TIFF at all)
- elif num_tiff_series == 1: # input is a single-series TIFF (output = input)
- try:
- os.symlink(args.input, args.output / '1.tiff')
- except OSError:
- shutil.copyfile(args.input, args.output / '1.tiff')
- else: # input is not a TIFF, conversion needed
- img = giatools.Image.read(args.input)
- OutputWriter(
- dir_path=args.output,
- num_images=1,
- squeeze=args.squeeze,
- verbose=False,
- ).write(
- img.squeeze_like(img.original_axes),
- )
+ # Extract the individual images
+ output = OutputWriter(
+ dir_path=args.output,
+ num_images=num_images,
+ squeeze=args.squeeze,
+ verbose=(num_images > 1),
+ offset=args.offset,
+ step=args.step,
+ count=args.count,
+ )
+ for position in range(num_images):
+ img = giatools.Image.read(args.input, position=position, normalize_axes=None)
+ output.write(img)
# If splitting along an image axes...
else:
@@ -105,6 +96,9 @@
num_images=arr.shape[0],
squeeze=args.squeeze,
verbose=False,
+ offset=args.offset,
+ step=args.step,
+ count=args.count,
)
for img_idx, img in enumerate(arr):
img = np.moveaxis(img[None], 0, axis_pos)
diff -r 227e8928af6e -r d45a07063da1 split_image.xml
--- a/split_image.xml Fri Dec 12 21:02:25 2025 +0000
+++ b/split_image.xml Sat Apr 04 21:22:07 2026 +0000
@@ -4,7 +4,7 @@
creators.xml
tests.xml
2.3.5
- 0
+ 1
@@ -18,9 +18,11 @@
numpy
- giatools
- tifffile
+ giatools
+
+
+
-
+
@@ -44,11 +52,17 @@
+ help="Only non-singleton axes (axes with more than one element) will be retained in the result images. This does not apply for the X and Y axes which always are retained."/>
+
+
+
-
+
@@ -66,7 +80,7 @@
-
+
@@ -79,8 +93,8 @@
-
-
+
+
@@ -88,17 +102,20 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
@@ -114,7 +131,7 @@
-
+
@@ -127,7 +144,7 @@
-
+
@@ -141,8 +158,8 @@
-
-
+
+
@@ -171,7 +188,7 @@
-
+
@@ -188,7 +205,7 @@
-
+
diff -r 227e8928af6e -r d45a07063da1 test-data/multiseries_series6.tiff
Binary file test-data/multiseries_series6.tiff has changed
diff -r 227e8928af6e -r d45a07063da1 test-data/zcyx_slice01.tiff
Binary file test-data/zcyx_slice01.tiff has changed
diff -r 227e8928af6e -r d45a07063da1 test-data/zcyx_slice10.tiff
Binary file test-data/zcyx_slice10.tiff has changed
diff -r 227e8928af6e -r d45a07063da1 test-data/zcyx_slice18.tiff
Binary file test-data/zcyx_slice18.tiff has changed
diff -r 227e8928af6e -r d45a07063da1 test-data/zcyx_slice25.tiff
Binary file test-data/zcyx_slice25.tiff has changed