diff options
Diffstat (limited to 'mypath/gui.py')
-rwxr-xr-x | mypath/gui.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/mypath/gui.py b/mypath/gui.py new file mode 100755 index 0000000..2835e96 --- /dev/null +++ b/mypath/gui.py | |||
@@ -0,0 +1,52 @@ | |||
1 | #!/usr/bin/env python | ||
2 | import matplotlib.pyplot as plt | ||
3 | from matplotlib.widgets import Button, TextBox | ||
4 | from tkinter.filedialog import askopenfilename | ||
5 | from scipy.ndimage import binary_dilation | ||
6 | import numpy as np | ||
7 | import os | ||
8 | |||
9 | def load_and_process_image(file_path, structure_size): | ||
10 | with open(file_path, 'r') as f: | ||
11 | f.readline() # 忽略第一行 | ||
12 | lines = f.readlines() | ||
13 | lines = lines[::-1] # 颠倒一下,第一行在最下面 | ||
14 | |||
15 | image_data = [list(map(int, line.strip().split())) for line in lines] | ||
16 | image_array = np.array(image_data) | ||
17 | structure = np.ones((structure_size, structure_size)) | ||
18 | image_array = binary_dilation(image_array, structure) | ||
19 | return image_array | ||
20 | |||
21 | def on_button_click(event): | ||
22 | global file_path | ||
23 | file_path = askopenfilename() | ||
24 | structure_size = int(text_box.text) | ||
25 | image_array = load_and_process_image(file_path, structure_size) | ||
26 | ax.cla() | ||
27 | ax.imshow(image_array, cmap='gray', interpolation='nearest') | ||
28 | ax.set_title(file_path) | ||
29 | fig.canvas.draw() | ||
30 | |||
31 | def update_cursor_position(event): | ||
32 | if event.inaxes == ax: | ||
33 | x, y = event.xdata, event.ydata | ||
34 | x -= 900 | ||
35 | y -= 900 | ||
36 | ax.set_title(f'{file_path} | Cursor: ({x:.2f}, {-y:.2f})') | ||
37 | fig.canvas.draw_idle() | ||
38 | |||
39 | if __name__ == '__main__': | ||
40 | fig, ax = plt.subplots() | ||
41 | ax_button = plt.axes([0.05, 0.85, 0.1, 0.075]) | ||
42 | button = Button(ax_button, 'Open') | ||
43 | file_path = os.getcwd() | ||
44 | button.on_clicked(on_button_click) | ||
45 | |||
46 | ax_text_box = plt.axes([0.05, 0.95, 0.1, 0.05]) | ||
47 | text_box = TextBox(ax_text_box, 'Size') | ||
48 | text_box.set_val(1) | ||
49 | |||
50 | fig.canvas.mpl_connect('motion_notify_event', update_cursor_position) | ||
51 | |||
52 | plt.show() \ No newline at end of file | ||