comparison 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
comparison
equal deleted inserted replaced
2:ac497ba6819d 3:ba2b1a6f1b84
1 import argparse 1 import argparse
2 import sys 2 import sys
3
4 import giatools.io
5 import numpy as np
3 import skimage.io 6 import skimage.io
4 import skimage.transform 7 import skimage.transform
5 import scipy.misc 8 import skimage.util
6 from PIL import Image 9 from PIL import Image
7 10
8 11
9 def scale_image(input_file, output_file, scale, order=1): 12 def scale_image(input_file, output_file, scale, order, antialias):
10 Image.MAX_IMAGE_PIXELS = 50000*50000 13 Image.MAX_IMAGE_PIXELS = 50000 * 50000
11 img_in = skimage.io.imread(input_file) 14 im = giatools.io.imread(input_file)
12 if order == 0: 15
13 interp = 'nearest' 16 # Parse `--scale` argument
14 elif order == 1:
15 interp = 'bilinear'
16 elif order == 2:
17 interp = 'bicubic'
18 if ',' in scale: 17 if ',' in scale:
19 scale = scale[1:-1].split(',') 18 scale = [float(s.strip()) for s in scale.split(',')]
20 scale = [int(i) for i in scale] 19 assert len(scale) <= im.ndim, f'Image has {im.ndim} axes, but scale factors were given for {len(scale)} axes.'
21 elif '.' in scale: 20 scale = scale + [1] * (im.ndim - len(scale))
21
22 else:
22 scale = float(scale) 23 scale = float(scale)
23 else: 24
24 scale = int(scale) 25 # For images with 3 or more axes, the last axis is assumed to correspond to channels
25 res = scipy.misc.imresize(img_in, scale, interp=interp) 26 if im.ndim >= 3:
27 scale = [scale] * (im.ndim - 1) + [1]
28
29 # Do the scaling
30 res = skimage.transform.rescale(im, scale, order, anti_aliasing=antialias, preserve_range=True)
31
32 # Preserve the `dtype` so that both brightness and range of values is preserved
33 if res.dtype != im.dtype:
34 if np.issubdtype(im.dtype, np.integer):
35 res = res.round()
36 res = res.astype(im.dtype)
37
38 # Save result
26 skimage.io.imsave(output_file, res) 39 skimage.io.imsave(output_file, res)
27 40
28 41
29 if __name__ == "__main__": 42 if __name__ == "__main__":
30 parser = argparse.ArgumentParser() 43 parser = argparse.ArgumentParser()
31 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') 44 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin)
32 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (PNG)') 45 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin)
33 parser.add_argument('scale', type=str, help='fraction scaling factor(float), percentage scaling factor(int), output size(tuple(height,width))') # integer option not implemented in galaxy wrapper 46 parser.add_argument('--scale', type=str, required=True)
34 parser.add_argument('order', type=int, default=1, help='interpolation method') 47 parser.add_argument('--order', type=int, required=True)
48 parser.add_argument('--antialias', default=False, action='store_true')
35 args = parser.parse_args() 49 args = parser.parse_args()
36 50
37 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order) 51 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order, args.antialias)