|
| 1 | +# -------- Activate virtual environment ------------------------- |
| 2 | +import os |
| 3 | +import ctypes |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +parentFolder = str(Path(__file__).parent.parent) |
| 7 | +activate_path = parentFolder + '\\env\\Scripts\\activate_this.py' |
| 8 | +if os.path.exists(activate_path): |
| 9 | + exec(open(activate_path).read(), {'__file__': activate_path}) |
| 10 | + print(f'Aivia virtual environment activated\nUsing python: {activate_path}') |
| 11 | +else: |
| 12 | + # Attempt to still run the script with main Aivia python interpreter |
| 13 | + error_mess = f'Error: {activate_path} was not found.\nPlease run the \'FirstTimeSetup.py\' script in Aivia first.' |
| 14 | + ans = ctypes.windll.user32.MessageBoxW(0, error_mess, 'Error', 1) |
| 15 | + if ans == 2: |
| 16 | + sys.exit(error_mess) |
| 17 | + print('\n'.join(['#' * 40, error_mess, |
| 18 | + 'Now trying to fallback on python environment specified in Aivia options > Advanced.', |
| 19 | + '#' * 40])) |
| 20 | +# --------------------------------------------------------------- |
| 21 | + |
1 | 22 | import tifffile |
2 | | -from tkinter import filedialog |
3 | | -import tkinter as tk |
4 | | -from tkinter import ttk |
| 23 | +import wx |
5 | 24 | import textwrap |
6 | 25 |
|
7 | | -""" |
8 | | -Simple script to read TIFF tags and display them in a tkinter window. |
9 | | -Run this with the commandline and a file dialog window will pop up. |
10 | | -
|
11 | | -Note: XML parts are broken into pieces (rows), which are themselves broken |
12 | | -if still longer than specified "max_char_len" value. |
13 | | -
|
14 | | -Requirements |
15 | | ------------- |
16 | | -tifffile (comes with Aivia installer) |
17 | | -tkinter (needs the standard version of python) |
18 | | -""" |
19 | | - |
20 | 26 | max_char_len = 150 |
21 | 27 |
|
| 28 | + |
| 29 | +# [INPUT Name:inputImagePath Type:string DisplayName:'Any channel'] |
| 30 | +# [OUTPUT Name:resultPath Type:string DisplayName:'Dummy to delete'] |
22 | 31 | def run(params): |
23 | | - # Retrieve file |
24 | | - root = tk.Tk() |
25 | | - root.withdraw() |
26 | | - img_path = filedialog.askopenfilename(title='Select TIF file to read tags', |
27 | | - filetype=[('TIF files', '.tif .tiff')]) |
| 32 | + # Choose file |
| 33 | + img_path = pick_file('') |
28 | 34 |
|
29 | 35 | # Read tags |
30 | | - with tifffile.TiffFile(img_path) as tif: |
| 36 | + with tifffile.TiffFile(img_path[0]) as tif: |
31 | 37 | tif_tags = {} |
32 | 38 | for tag in tif.pages[0].tags.values(): |
33 | 39 | name, value = tag.name, tag.value |
34 | 40 | tif_tags[name] = value |
35 | 41 | print(name, ': ', value) |
36 | 42 |
|
37 | | - # Display tags in table |
38 | | - master = tk.Tk() |
39 | | - master.bind('<Escape>', lambda e: master.quit()) # Press Esc to stop mainloop |
40 | | - tk.Label(master, text="Tiff Tags", font=("Arial", 14)).grid(row=0, column=0) |
41 | | - tk.Label(master, text="press Esc to close window", font=("Arial", 8)).grid(row=1, column=0) |
42 | | - cols = ('Tag name', 'Value') |
43 | | - tree = ttk.Treeview(master, columns=cols, show='headings') |
44 | | - for col in cols: |
45 | | - tree.heading(col, text=col) |
46 | | - tree.grid(row=2, column=0) |
| 43 | + # Display tags in table, preparing table |
| 44 | + app = wx.App() |
| 45 | + frame = wx.Frame(parent=None, title='TIF tags', size=(1000, 800)) |
| 46 | + # table = grid.Grid(frame) |
| 47 | + # table.CreateGrid(len(tif_tags) + 1, 2) |
| 48 | + table = wx.ListCtrl(frame, size=(-1, 100), style=wx.LC_REPORT) |
| 49 | + table.InsertColumn(0, 'Tag name', width=200) |
| 50 | + table.InsertColumn(1, 'Value', width=800) |
| 51 | + |
| 52 | + # Insert values |
| 53 | + i = 0 |
47 | 54 | for tag_name, tag_val in tif_tags.items(): |
| 55 | + # Insert tag name |
| 56 | + table.InsertItem(i, tag_name) |
| 57 | + |
| 58 | + # Set value because some can be very long |
48 | 59 | final_val = str(tag_val) |
| 60 | + |
| 61 | + # Replace returns |
| 62 | + final_val = final_val.replace('\n', '_') |
| 63 | + |
49 | 64 | if len(final_val) > max_char_len: |
50 | 65 | final_val_list = final_val.split('<') |
51 | 66 | for v in range(0, len(final_val_list)): |
| 67 | + i += 1 |
| 68 | + table.InsertItem(i, tag_name) |
52 | 69 | stri = final_val_list[v] |
53 | 70 | if len(stri) > 0: |
54 | 71 | if len(final_val_list) > 1: |
55 | 72 | stri = '<' + stri |
56 | 73 | if len(stri) < max_char_len: |
57 | | - tree.insert("", "end", values=(tag_name, stri)) |
| 74 | + table.SetItem(i, 1, stri) |
58 | 75 | else: |
59 | 76 | final_val_list2 = [stri[i:i + max_char_len] for i in range(0, len(stri), max_char_len)] |
60 | 77 | for w in range(0, len(final_val_list2)): |
61 | | - tree.insert("", "end", values=(tag_name, final_val_list2[w])) |
62 | | - |
| 78 | + i += 1 |
| 79 | + table.InsertItem(i, tag_name) |
| 80 | + table.SetItem(i, 1, final_val_list2[w]) |
63 | 81 | else: |
64 | | - tree.insert("", "end", values=(tag_name, final_val)) |
65 | | - tree.column(cols[1], width=1000, stretch=tk.YES) |
| 82 | + table.SetItem(i, 1, final_val) |
| 83 | + |
| 84 | + i += 1 |
| 85 | + |
| 86 | + frame.Show() |
| 87 | + app.MainLoop() |
66 | 88 |
|
67 | | - master.mainloop() |
68 | 89 |
|
| 90 | +def wrap(string, length): |
| 91 | + return '\n'.join(textwrap.wrap(string, length)) |
69 | 92 |
|
70 | | -def wrap(string, lenght): |
71 | | - return '\n'.join(textwrap.wrap(string, lenght)) |
| 93 | + |
| 94 | +def pick_file(default_dir): |
| 95 | + print('Starting wxPython app') |
| 96 | + app = wx.App() |
| 97 | + |
| 98 | + # Create open file dialog |
| 99 | + openFileDialog = wx.FileDialog(None, "Select an image", |
| 100 | + default_dir, "", "Image files (*.tif)|*.tif", |
| 101 | + wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) |
| 102 | + |
| 103 | + openFileDialog.ShowModal() |
| 104 | + filename = openFileDialog.GetPaths() |
| 105 | + print("Selected image: ", filename) |
| 106 | + openFileDialog.Destroy() |
| 107 | + return filename |
72 | 108 |
|
73 | 109 |
|
74 | 110 | if __name__ == '__main__': |
75 | 111 | params = {} |
76 | 112 | run(params) |
| 113 | + |
| 114 | +# CHANGELOG |
| 115 | +# - v1.00: - Using tkinter to choose file and display results |
| 116 | +# - v1.10: - Using wxpython to choose file and display results |
0 commit comments