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