annotate scale_image.py @ 3:ba2b1a6f1b84 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
author imgteam
date Thu, 17 Oct 2024 10:47:14 +0000
parents ac497ba6819d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 import sys
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
4 import giatools.io
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
5 import numpy as np
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import skimage.io
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import skimage.transform
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
8 import skimage.util
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 from PIL import Image
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
11
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
12 def scale_image(input_file, output_file, scale, order, antialias):
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
13 Image.MAX_IMAGE_PIXELS = 50000 * 50000
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
14 im = giatools.io.imread(input_file)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
15
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
16 # Parse `--scale` argument
2
ac497ba6819d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
17 if ',' in scale:
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
18 scale = [float(s.strip()) for s in scale.split(',')]
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
19 assert len(scale) <= im.ndim, f'Image has {im.ndim} axes, but scale factors were given for {len(scale)} axes.'
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
20 scale = scale + [1] * (im.ndim - len(scale))
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
21
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
22 else:
2
ac497ba6819d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
23 scale = float(scale)
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
24
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
25 # For images with 3 or more axes, the last axis is assumed to correspond to channels
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
26 if im.ndim >= 3:
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
27 scale = [scale] * (im.ndim - 1) + [1]
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
28
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
29 # Do the scaling
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
30 res = skimage.transform.rescale(im, scale, order, anti_aliasing=antialias, preserve_range=True)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
31
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
32 # Preserve the `dtype` so that both brightness and range of values is preserved
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
33 if res.dtype != im.dtype:
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
34 if np.issubdtype(im.dtype, np.integer):
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
35 res = res.round()
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
36 res = res.astype(im.dtype)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
37
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
38 # Save result
2
ac497ba6819d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
39 skimage.io.imsave(output_file, res)
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 if __name__ == "__main__":
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 parser = argparse.ArgumentParser()
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
44 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
45 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
46 parser.add_argument('--scale', type=str, required=True)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
47 parser.add_argument('--order', type=int, required=True)
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
48 parser.add_argument('--antialias', default=False, action='store_true')
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
49 args = parser.parse_args()
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
50
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
51 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order, args.antialias)