annotate scale_image.py @ 5:85666e555698 draft

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