annotate scale_image.py @ 4:3179853faae9 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:26:23 +0000
parents d09507d3fb0e
children 85666e555698
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
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
4 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
5 import skimage.io
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 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
7 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
8 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
9
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
10
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
11 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
12 Image.MAX_IMAGE_PIXELS = 50000 * 50000
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
13 im = skimage.io.imread(input_file)
3179853faae9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 3
diff changeset
14
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 # 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
16 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
17 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
18 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
19 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
20
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 else:
2
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
22 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
23
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 # 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
25 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
26 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
27
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 # 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
29 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
30
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 # 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
32 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
33 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
34 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
35 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
36
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 # 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
38 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
39
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 if __name__ == "__main__":
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49
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
50 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order, args.antialias)