-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowImage
executable file
·128 lines (104 loc) · 4.04 KB
/
showImage
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# remove the necessity to have .py extension
from PIL import Image
import sys
import os.path
import numpy as np
import math
import matplotlib.pyplot as plt # Just so we can visually confirm we have the same images
option_handle_list = ['--showFirstImage', '--showImage', '--stats']
option_unique_list = ['--info', '--showAllImages', '--verbose']
options = {}
for option_handle in option_handle_list:
if option_handle in sys.argv:
options[option_handle[2:]] = sys.argv[sys.argv.index(option_handle) + 1]
else:
options[option_handle[2:]] = None
for option_handle in option_unique_list:
if option_handle in sys.argv:
options[option_handle[2:]] = True
else:
options[option_handle[2:]] = None
if options['info'] != None:
print("Function showImage, this function can be used to show npy or tiff images")
print("USAGE : showImage [imagePath] [options]")
print("")
print("options")
print(" --info: get info about the image file")
print(" --showImage N: show image number N")
print(" --showAllImages: show all images")
print(" --stats N: get stats of image number N, including its histogram")
exit()
if not os.path.exists(sys.argv[1]):
raise ValueError("Incorrect path, image path must be the first argument")
imagePath = sys.argv[1]
if options['showImage'] == None and options['showAllImages'] == None and options['stats'] == None:
options['showAllImages'] = True #default
if options['verbose']:
np.set_printoptions(threshold=sys.maxsize) #writing entire data (not splitting) may consume time
if ".npy" in imagePath:
images = np.load(imagePath)
else:
img = Image.open(imagePath)
print("number of images found : ", img.n_frames)
images = []
for i in range(img.n_frames): #number of images
img.seek(i)
if options['verbose']:
print("----------- image " + str(i) + "-----------")
print("format : " + str(img.format))
print("size : " + str(img.size))
print("mode : " + str(img.mode))
npyImage = np.array(img)
images.append(npyImage)
if options['stats'] != None:
imageNumber = int(options['stats'])
if options['verbose']:
for i in range(img.n_frames):
max = np.amax(images[i])
min = np.amin(images[i])
val = np.arange(max)
print("Range (", i, "):", min, max)
valuesImage = images[imageNumber].flatten()
max = np.amax(images[imageNumber])
min = np.amin(images[imageNumber])
val = np.arange(max)
print("Range :", min, max)
print("flat : ", valuesImage)
histo, bins = np.histogram(valuesImage, bins = max)
plt.hist(histo, bins='auto')
#plt.yscale("log")
plt.show()
if options['showImage'] != None:
try:
imageNumber = int(options['showImage'])
# #images[imageNumber][y][x]
# print(images[imageNumber][345][138]) #y
# print(images[imageNumber][150][160]) #y
# print(images[imageNumber][169][193]) #y
# getRGB(images[imageNumber][169][193]) #y
# print("-----")
# print(images[imageNumber][212][392]) #n
# print(images[imageNumber][68][49]) #n
if ".npy" in imagePath:
plt.imshow(np.squeeze(images[imageNumber])) #removing Z axis
else:
plt.imshow(images[imageNumber])
plt.show()
except ValueError:
print('parameter after --showImage must be a number between 0 and ' + str(len(images)))
if options['showAllImages'] != None:
print("showing ", len(images), " images")
imageNbToShow = len(images)
if len(images) > 36:
imageNbToShow = 36
print("Too many images (", len(images), "). Showing only the 36 first images")
sqrt = math.ceil(math.sqrt(imageNbToShow))
fig = plt.figure(figsize=(sqrt, sqrt))
for i in range(1, imageNbToShow + 1):
fig.add_subplot(sqrt, sqrt, i)
if ".npy" in imagePath:
plt.imshow(np.squeeze(images[i - 1])) #removing Z axis
else:
plt.imshow(images[i - 1])
plt.show()