diff --git a/macros/tests.xml b/macros/tests.xml index d93d9afb..45bb404f 100644 --- a/macros/tests.xml +++ b/macros/tests.xml @@ -21,7 +21,11 @@ token_metric="iou" token_eps="0.01"> - + + + + + @@ -31,7 +35,11 @@ token_metric="rms" token_eps="0.01"> - + + + + + diff --git a/tools/2d_simple_filter/filter.xml b/tools/2d_simple_filter/filter.xml index b2b58e9c..05cea9c8 100644 --- a/tools/2d_simple_filter/filter.xml +++ b/tools/2d_simple_filter/filter.xml @@ -102,6 +102,9 @@ **Applies a standard filter to a single-channel 2-D image.** + Note that this operation can change the range of the values of the image intensities in the image. + This is necessary, for example, to preserve the brightness of the image after filtering. + 10.1016/j.jbiotec.2017.07.019 diff --git a/tools/scale_image/creators.xml b/tools/scale_image/creators.xml new file mode 120000 index 00000000..5d2b71e0 --- /dev/null +++ b/tools/scale_image/creators.xml @@ -0,0 +1 @@ +../../macros/creators.xml \ No newline at end of file diff --git a/tools/scale_image/scale_image.py b/tools/scale_image/scale_image.py index 123e1311..d42875d9 100644 --- a/tools/scale_image/scale_image.py +++ b/tools/scale_image/scale_image.py @@ -1,38 +1,50 @@ import argparse import sys -import scipy.misc +import numpy as np import skimage.io import skimage.transform +import skimage.util from PIL import Image -def scale_image(input_file, output_file, scale, order=1): +def scale_image(input_file, output_file, scale, order, antialias): 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' + im = skimage.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(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 = int(scale) - res = scipy.misc.imresize(img_in, scale, interp=interp) + scale = float(scale) + + # Do the scaling + res = skimage.transform.rescale(im, scale, order, anti_aliasing=antialias) + + # Convert `res` to the same dtype as `im`, + # it is important to preserve the dtype so that both brightness and range of values is preserved + if res.dtype != im.dtype: + src_max_value = skimage.util.dtype_limits(res)[1] + dst_max_value = skimage.util.dtype_limits(im)[1] + res = (res / src_max_value) * dst_max_value + 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) diff --git a/tools/scale_image/scale_image.xml b/tools/scale_image/scale_image.xml index dcc11523..0ccb94fa 100644 --- a/tools/scale_image/scale_image.xml +++ b/tools/scale_image/scale_image.xml @@ -1,5 +1,14 @@ - + with scikit-image + + creators.xml + tests.xml + 0.18.3 + 0 + + + + operation_3443 @@ -8,64 +17,85 @@ scikit-image - pillow - scikit-image - numpy - scipy - tifffile + scikit-image + pillow + numpy + tifffile - - - + python '$__tool_directory__/scale_image.py' '$input' + + ./output.${input.ext} + + --scale '$scale' + --order $order + $antialias + + && mv ./output.${input.ext} ./output + + ]]> - - - - - - - - - - - - - - + + + - + + - - - - - - + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - **What it does** - This tool scales an image using the scaling factor. + **Scales an image using one or more scaling factors.** + + The image is rescaled uniformly along all axes, or anistropically if multiple scale factors are given. + + Note that this operation can change the range of the values of the image intensities in the image. + This is necessary, for example, to preserve the brightness of the image after scaling. + 10.1016/j.jbiotec.2017.07.019 diff --git a/tools/scale_image/test-data/anisotropic.png b/tools/scale_image/test-data/anisotropic.png new file mode 100644 index 00000000..6d9e4a8a Binary files /dev/null and b/tools/scale_image/test-data/anisotropic.png differ diff --git a/tools/scale_image/test-data/input1_binary_rgb.png b/tools/scale_image/test-data/input1_binary_rgb.png new file mode 100644 index 00000000..221ab286 Binary files /dev/null and b/tools/scale_image/test-data/input1_binary_rgb.png differ diff --git a/tools/scale_image/test-data/input2_normalized.tiff b/tools/scale_image/test-data/input2_normalized.tiff new file mode 100644 index 00000000..f33d8ba9 Binary files /dev/null and b/tools/scale_image/test-data/input2_normalized.tiff differ diff --git a/tools/scale_image/test-data/input3_not_normalized.tiff b/tools/scale_image/test-data/input3_not_normalized.tiff new file mode 100644 index 00000000..33cf72c7 Binary files /dev/null and b/tools/scale_image/test-data/input3_not_normalized.tiff differ diff --git a/tools/scale_image/test-data/normalized.tiff b/tools/scale_image/test-data/normalized.tiff new file mode 100644 index 00000000..21f0e11b Binary files /dev/null and b/tools/scale_image/test-data/normalized.tiff differ diff --git a/tools/scale_image/test-data/not_normalized.tiff b/tools/scale_image/test-data/not_normalized.tiff new file mode 100644 index 00000000..a63a6656 Binary files /dev/null and b/tools/scale_image/test-data/not_normalized.tiff differ diff --git a/tools/scale_image/test-data/out.png b/tools/scale_image/test-data/out.png deleted file mode 100644 index 00bef48f..00000000 Binary files a/tools/scale_image/test-data/out.png and /dev/null differ diff --git a/tools/scale_image/test-data/out2.png b/tools/scale_image/test-data/out2.png deleted file mode 100644 index 00bef48f..00000000 Binary files a/tools/scale_image/test-data/out2.png and /dev/null differ diff --git a/tools/scale_image/test-data/sample1.png b/tools/scale_image/test-data/sample1.png deleted file mode 100644 index 2283a3b6..00000000 Binary files a/tools/scale_image/test-data/sample1.png and /dev/null differ diff --git a/tools/scale_image/test-data/uniform_binary.png b/tools/scale_image/test-data/uniform_binary.png new file mode 100644 index 00000000..8a0381b1 Binary files /dev/null and b/tools/scale_image/test-data/uniform_binary.png differ diff --git a/tools/scale_image/tests.xml b/tools/scale_image/tests.xml new file mode 120000 index 00000000..e20d710a --- /dev/null +++ b/tools/scale_image/tests.xml @@ -0,0 +1 @@ +../../macros/tests.xml \ No newline at end of file