-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (74 loc) · 3.53 KB
/
main.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
from tkinterdnd2 import DND_FILES, TkinterDnD
from tkinter import filedialog, Frame, Button, Label
from tkinter import ttk
from PIL import Image, ImageTk
import os
import rawpy
import imageio
class CR3Viewer:
def __init__(self):
self.root = TkinterDnD.Tk()
self.root.title("CR3 Viewer")
self.top_frame = Frame(self.root)
self.top_frame.pack(side="top", fill="x")
self.new_button = Button(self.top_frame, text="New", command=self.open_files)
self.new_button.pack(side="left")
self.save_button = Button(self.top_frame, text="Save", command=self.save_image)
self.save_button.pack(side="left")
self.close_button = Button(self.top_frame, text="Close", command=self.close_current_tab)
self.close_button.pack(side="left")
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(expand=True, fill='both')
self.root.bind("<Right>", self.switch_tab_right)
self.root.bind("<Left>", self.switch_tab_left)
self.root.bind("<Control-s>", self.save_image)
self.root.drop_target_register(DND_FILES)
self.root.dnd_bind('<<Drop>>', self.on_drop)
def open_files(self, file_paths=None):
if file_paths is None:
file_paths = filedialog.askopenfilenames(title="Select CR3 Files", filetypes=[("CR3 files", "*.cr3")])
for file_path in file_paths:
self.add_tab(file_path)
def add_tab(self, file_path):
frame = Frame(self.notebook)
self.notebook.add(frame, text=os.path.basename(file_path))
self.update_image(frame, file_path)
def update_image(self, frame, file_path):
with rawpy.imread(file_path) as raw:
rgb = raw.postprocess()
image = Image.fromarray(rgb)
image.thumbnail((800, 800), Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(image)
label = Label(frame, image=tkimage)
label.image = tkimage
label.pack()
def save_image(self, event=None):
current_tab_index = self.notebook.index(self.notebook.select())
frame = self.notebook.nametowidget(self.notebook.select())
file_path = self.notebook.tab(current_tab_index, "text")
cr3_file_path = os.path.join("path/to/your/cr3/files", file_path)
save_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG", "*.jpg"), ("PNG", "*.png")])
if save_path:
with rawpy.imread(cr3_file_path) as raw:
rgb = raw.postprocess()
imageio.imsave(save_path, rgb)
print(f"Image saved as {save_path}")
def switch_tab_right(self, event):
current_tab_index = self.notebook.index(self.notebook.select())
next_tab_index = (current_tab_index + 1) % self.notebook.index("end")
self.notebook.select(next_tab_index)
def switch_tab_left(self, event):
current_tab_index = self.notebook.index(self.notebook.select())
prev_tab_index = (current_tab_index - 1) % self.notebook.index("end")
self.notebook.select(prev_tab_index)
def on_drop(self, event):
file_paths = self.root.tk.splitlist(event.data)
self.open_files(file_paths)
def close_current_tab(self):
"""Closes the currently selected tab in the notebook."""
current_tab = self.notebook.select()
if current_tab:
self.notebook.forget(current_tab)
if __name__ == "__main__":
viewer = CR3Viewer()
viewer.root.mainloop()