Skip to content

Commit dd3868d

Browse files
authored
Update ReadTiffTags.py
v1.10
1 parent c24636d commit dd3868d

File tree

1 file changed

+80
-40
lines changed

1 file changed

+80
-40
lines changed

Utilities/ReadTiffTags.py

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,116 @@
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+
122
import tifffile
2-
from tkinter import filedialog
3-
import tkinter as tk
4-
from tkinter import ttk
23+
import wx
524
import textwrap
625

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-
2026
max_char_len = 150
2127

28+
29+
# [INPUT Name:inputImagePath Type:string DisplayName:'Any channel']
30+
# [OUTPUT Name:resultPath Type:string DisplayName:'Dummy to delete']
2231
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('')
2834

2935
# Read tags
30-
with tifffile.TiffFile(img_path) as tif:
36+
with tifffile.TiffFile(img_path[0]) as tif:
3137
tif_tags = {}
3238
for tag in tif.pages[0].tags.values():
3339
name, value = tag.name, tag.value
3440
tif_tags[name] = value
3541
print(name, ': ', value)
3642

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
4754
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
4859
final_val = str(tag_val)
60+
61+
# Replace returns
62+
final_val = final_val.replace('\n', '_')
63+
4964
if len(final_val) > max_char_len:
5065
final_val_list = final_val.split('<')
5166
for v in range(0, len(final_val_list)):
67+
i += 1
68+
table.InsertItem(i, tag_name)
5269
stri = final_val_list[v]
5370
if len(stri) > 0:
5471
if len(final_val_list) > 1:
5572
stri = '<' + stri
5673
if len(stri) < max_char_len:
57-
tree.insert("", "end", values=(tag_name, stri))
74+
table.SetItem(i, 1, stri)
5875
else:
5976
final_val_list2 = [stri[i:i + max_char_len] for i in range(0, len(stri), max_char_len)]
6077
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])
6381
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()
6688

67-
master.mainloop()
6889

90+
def wrap(string, length):
91+
return '\n'.join(textwrap.wrap(string, length))
6992

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
72108

73109

74110
if __name__ == '__main__':
75111
params = {}
76112
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

Comments
 (0)