-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmcrop.py
executable file
·101 lines (80 loc) · 3.2 KB
/
mmcrop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
import argparse
from mmtools import stack, log
# default values
input_filename = None
output_filename = None
output_suffix = "_crop.tif"
channel = None
reverse_channel = False
crop_area = None
z_range = None
t_range = None
# parse arguments
parser = argparse.ArgumentParser(description='Crop a multi-page TIFF image.', \
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-o', '--output-file', default = output_filename, \
help='output image filename. [basename]{0} by default'.format(output_suffix))
parser.add_argument('-r', '--reverse-channel', action = 'store_true', \
help='reverse the order of channels before selecting channel(s).')
parser.add_argument('-c', '--channel', type = int, default = channel, \
help='specify the channel to process (minus index to remove).')
parser.add_argument('-R', '--crop-area', type = int, nargs = 4, default = crop_area, \
metavar = ('X', 'Y', 'W', "H"),
help='Crop using the specified area.')
parser.add_argument('-z', '--z-range', type = int, nargs = 2, default = z_range, \
metavar = ('START', 'END'),
help='Specify the range of z planes to output')
parser.add_argument('-t', '--t-range', type = int, nargs = 2, default = t_range, \
metavar = ('START', 'END'),
help='Specify the range of time frames to output')
log.add_argument(parser)
parser.add_argument('input_file', default = input_filename, \
help='input (multipage) TIFF file')
args = parser.parse_args()
# logging
logger = log.get_logger(__file__, level = args.log_level)
# set arguments
input_filename = args.input_file
channel = args.channel
reverse_channel = args.reverse_channel
z_range = args.z_range
t_range = args.t_range
crop_area = args.crop_area
if args.output_file is None:
output_filename = stack.with_suffix(input_filename, output_suffix)
else:
output_filename = args.output_file
# read an image file
input_stack = stack.Stack(input_filename)
# slices
if t_range is None:
t_slice = slice(0, input_stack.t_count, 1)
else:
t_slice = slice(t_range[0], t_range[1] + 1, 1)
if channel is None:
c_slice = [i for i in range(input_stack.c_count)]
else:
if channel >= 0:
c_slice = [channel]
else:
c_slice = [i for i in range(input_stack.c_count) if i != abs(channel)]
if reverse_channel:
c_slice = c_slice[::-1]
if z_range is None:
z_slice = slice(0, input_stack.z_count, 1)
else:
z_slice = slice(z_range[0], z_range[1] + 1, 1)
if crop_area is None:
x_slice = slice(0, input_stack.width, 1)
y_slice = slice(0, input_stack.height, 1)
else:
x_slice = slice(crop_area[0], crop_area[0] + crop_area[2], 1)
y_slice = slice(crop_area[1], crop_area[1] + crop_area[3], 1)
# crop TIFF
logger.info("Cropping using area: {0}".format(crop_area))
logger.info("Slices: {0}".format([t_slice, c_slice, z_slice, y_slice, x_slice]))
input_stack.crop_by_slice([t_slice, c_slice, z_slice, y_slice, x_slice])
# output TIFF
logger.info("Output image: {0}".format(output_filename))
input_stack.save_ome_tiff(output_filename)