annotate scale_image.py @ 3:d09507d3fb0e draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit 6b687746bdb3d5d1fb11ecffd6dd1bf42dc2c38d
author imgteam
date Fri, 10 Nov 2023 14:23:33 +0000
parents f3c05a734dd1
children 3179853faae9
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
d09507d3fb0e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit 6b687746bdb3d5d1fb11ecffd6dd1bf42dc2c38d
imgteam
parents: 2
diff changeset
4 import scipy.misc
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
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 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
8
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
9
0
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 def scale_image(input_file, output_file, scale, order=1):
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
11 Image.MAX_IMAGE_PIXELS = 50000 * 50000
2
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
12 img_in = skimage.io.imread(input_file)
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
13 if order == 0:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
14 interp = 'nearest'
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
15 elif order == 1:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
16 interp = 'bilinear'
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
17 elif order == 2:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
18 interp = 'bicubic'
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
19 if ',' in scale:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
20 scale = scale[1:-1].split(',')
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
21 scale = [int(i) for i in scale]
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
22 elif '.' in scale:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
23 scale = float(scale)
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
24 else:
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
25 scale = int(scale)
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
26 res = scipy.misc.imresize(img_in, scale, interp=interp)
f3c05a734dd1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 1
diff changeset
27 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
28
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 if __name__ == "__main__":
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31 parser = argparse.ArgumentParser()
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
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
33 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (PNG)')
d09507d3fb0e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit 6b687746bdb3d5d1fb11ecffd6dd1bf42dc2c38d
imgteam
parents: 2
diff changeset
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
0
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35 parser.add_argument('order', type=int, default=1, help='interpolation method')
c4c76f1ebad2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 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
37
1
3e4231ed875e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit aeb095d3a95eff1cda6127a8dd757552c0405a11
imgteam
parents: 0
diff changeset
38 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order)