diff 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
line wrap: on
line diff
--- a/scale_image.py	Mon Jul 22 04:59:15 2019 -0400
+++ b/scale_image.py	Thu Oct 17 10:47:14 2024 +0000
@@ -1,37 +1,51 @@
 import argparse
 import sys
+
+import giatools.io
+import numpy as np
 import skimage.io
 import skimage.transform
-import scipy.misc
+import skimage.util
 from PIL import Image
 
- 
-def scale_image(input_file, output_file, scale, order=1):
-    Image.MAX_IMAGE_PIXELS = 50000*50000
-    img_in = skimage.io.imread(input_file)
-    if order == 0:
-        interp = 'nearest'
-    elif order == 1:
-        interp = 'bilinear'
-    elif order == 2:
-        interp = 'bicubic'
+
+def scale_image(input_file, output_file, scale, order, antialias):
+    Image.MAX_IMAGE_PIXELS = 50000 * 50000
+    im = giatools.io.imread(input_file)
+
+    # Parse `--scale` argument
     if ',' in scale:
-        scale = scale[1:-1].split(',')
-        scale = [int(i) for i in scale]
-    elif '.' in scale:
+        scale = [float(s.strip()) for s in scale.split(',')]
+        assert len(scale) <= im.ndim, f'Image has {im.ndim} axes, but scale factors were given for {len(scale)} axes.'
+        scale = scale + [1] * (im.ndim - len(scale))
+
+    else:
         scale = float(scale)
-    else:
-        scale = int(scale)
-    res = scipy.misc.imresize(img_in, scale, interp=interp)
+
+        # For images with 3 or more axes, the last axis is assumed to correspond to channels
+        if im.ndim >= 3:
+            scale = [scale] * (im.ndim - 1) + [1]
+
+    # Do the scaling
+    res = skimage.transform.rescale(im, scale, order, anti_aliasing=antialias, preserve_range=True)
+
+    # Preserve the `dtype` so that both brightness and range of values is preserved
+    if res.dtype != im.dtype:
+        if np.issubdtype(im.dtype, np.integer):
+            res = res.round()
+        res = res.astype(im.dtype)
+
+    # Save result
     skimage.io.imsave(output_file, res)
 
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
-    parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
-    parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (PNG)') 
-    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
-    parser.add_argument('order', type=int, default=1, help='interpolation method')
+    parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin)
+    parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin)
+    parser.add_argument('--scale', type=str, required=True)
+    parser.add_argument('--order', type=int, required=True)
+    parser.add_argument('--antialias', default=False, action='store_true')
     args = parser.parse_args()
 
-    scale_image(args.input_file.name, args.out_file.name, args.scale, args.order)
+    scale_image(args.input_file.name, args.out_file.name, args.scale, args.order, args.antialias)