-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.py
executable file
·86 lines (67 loc) · 2.31 KB
/
show.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
#!/usr/bin/env python3
import sys
import os
import numpy as np
import argparse
import SimpleITK as sitk
import functools
from PIL import Image
def get_chunks(l, n):
n = max(1, n)
return (l[i:i + n] for i in range(0, len(l), n))
def get_concat_h(im1, im2):
dst = Image.new('L', (im1.width + im2.width, max(im1.height, im2.height)))
dst.paste(im1, (0, 0))
dst.paste(im2, (im1.width, 0))
return dst
def get_concat_v(im1, im2):
dst = Image.new('L', (max(im1.width, im2.width), im1.height + im2.height))
dst.paste(im1, (0, 0))
dst.paste(im2, (0, im1.height))
return dst
def showNumpyArray(filePath, width, height):
array = np.load(filePath)
Image.fromarray(array).resize((width, height)).show()
def showScan(filePath, width, height):
ct_scan = sitk.GetArrayFromImage(sitk.ReadImage(filePath))
images = list(
map(lambda x: Image.fromarray(x).resize((width, height)), ct_scan))
chunks = get_chunks(images, 20)
result = Image.new('L', (0, 0))
for chunk in chunks:
row = Image.new('L', (0, 0))
for image in chunk:
row = get_concat_h(row, image)
result = get_concat_v(result, row)
result.show(title=filePath)
def main():
parser = argparse.ArgumentParser(description='Show images.')
parser.add_argument('relativePath',
type=str,
help='Relative file/folder path')
parser.add_argument('-w',
'--width',
default=50,
type=int,
nargs='?',
help='Width in inches.')
parser.add_argument('-hg',
'--height',
default=50,
type=int,
nargs='?',
help='Height in inches.')
args = parser.parse_args()
path = os.path.join(os.getcwd(), args.relativePath)
if not os.path.exists(path):
print("File don't exists: {}".format(path))
exit()
if path.endswith('npy'):
showNumpyArray(path, args.width, args.height)
elif path.endswith('mhd'):
showScan(path, args.width, args.height)
else:
print("Unknown file extension: {}".format(args.relativePath))
exit()
if __name__ == "__main__":
main()