-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapotome-merge.py
executable file
·70 lines (57 loc) · 2.47 KB
/
apotome-merge.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
#!/usr/bin/env python
import argparse
import re
import sys
from wand.image import Image
from wand.color import Color
from wand.display import display
parser = argparse.ArgumentParser(description="Tool for merging RGB TIFF images taken with a ZEISS fluorescence microscope")
parser.add_argument("-p", "--preview", action="store_true", help="Display the resulting image but do not save it")
parser.add_argument("-v", "--verbose", action="store_true", help="Produce more output")
parser.add_argument("green_channel_image")
parser.add_argument("red_channel_image")
args = parser.parse_args()
suffix = '_composite.tiff'
pattern = re.compile(r'\.tiff?$')
# Sanity check
if pattern.search(args.green_channel_image):
outfile = re.sub(pattern, suffix, args.green_channel_image)
else:
sys.stderr.write("Output filename would overwrite input filename. Not a TIFF file? No action taken.\n")
exit(1)
# We have to use a multi-staged approach with wand.display.composite_channel
# because wand 0.3.4. doesn't have ImageMagick's SetImageAlphaChannel or any
# real way to directly modify channels. The more efficient approach may
# actually be shelling out to ImageMagick proper.
# Open green_channel_image
with Image(filename=args.green_channel_image) as green:
if args.verbose:
print args.green_channel_image
for key, value in green.metadata.items():
print(key, value)
# Open red_channel_image
with Image(filename=args.red_channel_image) as red:
if args.verbose:
print args.red_channel_image
for key, value in green.metadata.items():
print(key, value)
# Clone green_channel_image to "result" to ensure we're non-destructive
with green.clone() as result:
# Receive only the red channel from red_channel_image onto result
result.composite_channel(
channel='all_channels',
image=red,
operator='copy_red')
# Remove blue channel from result by compositing it with a minus
# operation onto final
with result.clone() as final:
final.composite_channel(
channel='blue',
image=result,
operator='minus')
if args.preview:
display(final)
else:
final.save(filename=outfile)
if args.verbose:
print "Composite image saved to", outfile