Mercurial > repos > thomaswollmann > scale_image
annotate scale_image.py @ 0:35ad679613a7 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
author | thomaswollmann |
---|---|
date | Mon, 07 Jan 2019 04:59:02 -0500 |
parents | |
children |
rev | line source |
---|---|
0
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
1 import argparse |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
2 import sys |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
3 import skimage.io |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
4 import skimage.transform |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
5 import scipy.misc |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
6 import warnings |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
7 import os |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
8 from PIL import Image |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
9 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
10 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
11 def scale_image(input_file, output_file, scale, order=1): |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
12 with warnings.catch_warnings(): |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
13 warnings.simplefilter("ignore") |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
14 Image.MAX_IMAGE_PIXELS = 50000*50000 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
15 img_in = skimage.io.imread(input_file) |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
16 if order == 0: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
17 interp = 'nearest' |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
18 elif order == 1: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
19 interp = 'bilinear' |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
20 elif order == 2: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
21 interp = 'bicubic' |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
22 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
23 if ',' in scale: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
24 scale = scale[1:-1].split(',') |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
25 scale.reverse() |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
26 scale = [int(i) for i in scale] |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
27 elif '.' in scale: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
28 scale = float(scale) |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
29 else: |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
30 scale = int(scale) |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
31 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
32 res = scipy.misc.imresize(img_in, scale, interp=interp) |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
33 skimage.io.imsave(output_file, res) |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
34 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
35 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
36 if __name__ == "__main__": |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
37 parser = argparse.ArgumentParser() |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
38 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
39 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (PNG)') |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
40 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 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
41 parser.add_argument('order', type=int, default=1, help='interpolation method') |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
42 args = parser.parse_args() |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
43 |
35ad679613a7
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
thomaswollmann
parents:
diff
changeset
|
44 scale_image(args.input_file.name, args.out_file.name, args.scale, args.order) |