-
Notifications
You must be signed in to change notification settings - Fork 6
/
npy_image_viewer.py
124 lines (99 loc) · 3.13 KB
/
npy_image_viewer.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
This script plots a 2D array as an image.
It also gives statistics about the array.
If multiple files are in parameters, then you can switch between files with the arrows of the figure,
or with the left/right, up/down keys.
"""
import os
import sys
print("Python version:",sys.version)
print("Python interpreter:",sys.executable)
try:
import matplotlib.pyplot as plt
import numpy as np
except ImportError as error:
print(error)
input("Press any key to exit")
exit()
# Choose your backend
# plt.switch_backend("Qt4Agg")
# plt.switch_backend("TkAgg")
num_files = len(sys.argv)-1
f_index = 0
fig = plt.figure()
data = plt.imshow(np.load(sys.argv[1]),cmap='gray')
def print_info(A,file):
print(os.path.basename(file), " :\n")
print(A)
print("shape \t=", A.shape)
print("min \t=", np.min(A))
print("max \t=", np.max(A))
print("abs_min\t=", np.min(np.abs(A)))
print("mean \t=", np.mean(A))
print("median \t=", np.median(A))
print("std_dev\t=", np.std(A))
print("") # Jumps one line
# print("\n") # Jumps two lines
def callback_left_button(event):
""" this function gets called if we hit the left button"""
# print('Left button pressed')
global f_index
if f_index == 1:
return
else:
f_index -= 1
file = sys.argv[f_index]
A = np.load(file)
data.set_data(A)
plt.title(os.path.basename(file))
# Redraw
fig.canvas.draw()
fig.canvas.flush_events()
# Print image information
print_info(A,file)
def callback_right_button(event):
""" this function gets called if we hit the left button"""
# print('Right button pressed')
global f_index
global data
if f_index == num_files:
return
else:
f_index += 1
file = sys.argv[f_index]
A = np.load(file)
data.set_data(A)
plt.title(os.path.basename(file))
# Redraw
fig.canvas.draw()
fig.canvas.flush_events()
# Print image information
print_info(A,file)
# Display first image (avoid duplicating code)
callback_right_button(None)
# Rewire keyboard events
def on_keyboard(event):
if event.key == 'left' or event.key == 'up':
callback_left_button(event)
elif event.key == 'right' or event.key == 'down':
callback_right_button(event)
plt.gcf().canvas.mpl_connect('key_press_event', on_keyboard)
# Rewire button actions
if(plt.get_backend() == "Qt4Agg"):
toolbar_elements = fig.canvas.toolbar.children()
left_button = toolbar_elements[6]
right_button = toolbar_elements[8]
left_button.clicked.connect(callback_left_button)
right_button.clicked.connect(callback_right_button)
# # This doesn't work yet.
# if(plt.get_backend() == "TkAgg"):
# toolbar_elements = fig.canvas.toolbar.winfo_children() # With tkinter backend
# left_button = toolbar_elements[6]
# right_button = toolbar_elements[8]
# left_button.config(command=callback_left_button) # Doesn't work while debugging, but works if not debugging
# right_button.config(command=callback_right_button)
# GUI main loop
plt.show()
if sys.platform.startswith('linux'):
input("Hit Enter to quit ...")