source: ammosreader/sample_scripts/ammos_viewer.py@ 09b290d

AmmosSource guix
Last change on this file since 09b290d was 09b290d, checked in by Enrico Schwass <ennoausberlin@…>, 3 years ago

respect cycle and offset entry on click on update button

  • Property mode set to 100644
File size: 3.6 KB
Line 
1"""I provide a simple bit viewer specialized for AMMOS files."""
2
3import tkinter as tk
4from PIL import Image, ImageTk
5from random import randbytes
6
7from ammosreader.AmmosAudioReader import AmmosAudioReader
8
9class MainApplication(tk.Frame):
10 """I implement a simple bit viewer specialized for AMMOS files."""
11
12 def __init__(self, parent):
13 """I create the application window."""
14 super().__init__(parent)
15
16 self.parent = parent
17 self.file_name = "../sample_data/audio_data_sample.bin"
18 self.ammos_container = AmmosAudioReader(self.file_name).read_all_frames_left()
19 self.file = open(self.file_name, 'rb')
20 self.bytes = self.file.read(-1)
21 self.cycle = tk.IntVar()
22 self.cycle.set(self.ammos_container.unique_frame_sizes()[0])
23 self.cache = tk.IntVar()
24 self.cache.set(500000000)
25 self.canvas_size = (800, 600)
26 self.offset = tk.IntVar()
27 self.offset.set(0)
28 self.buildup()
29
30 def buildup(self):
31 """I build up the initial user interface."""
32 self.parent.title("File name:" + self.file_name + " Size:" + str(len(self.bytes)))
33 self.pack(fill="both", expand=True)
34 self.default_button_width = 10
35 image_frame = tk.Frame(self)
36 image_frame.pack(side=tk.LEFT)
37 self.canvas = tk.Canvas(image_frame, bg="#000000", width=self.canvas_size[0], height=self.canvas_size[1])
38 self.canvas.pack()
39 pil_image = Image.frombytes("L", self.canvas_size, self.current_bytes())
40 self.image = ImageTk.PhotoImage(pil_image)
41 # pil_image.show()
42 button_frame = tk.Frame(self)
43 self.image_id = self.canvas.create_image(0, 0, anchor='nw', image=self.image)
44 button_frame.pack(side="right")
45
46 tk.Label(button_frame, text='Cycle in bytes').pack(side=tk.TOP)
47 self.cycle_entry = tk.Entry(button_frame, text=self.cycle)
48 self.cycle_entry.pack(side=tk.TOP)
49
50 tk.Label(button_frame, text='Cache in bytes').pack(side=tk.TOP)
51 self.cache_entry = tk.Entry(button_frame, text=self.cache)
52 self.cache_entry.pack(side=tk.TOP)
53
54 tk.Label(button_frame, text='Offset in bytes').pack(side=tk.TOP)
55 self.offset_entry = tk.Entry(button_frame, text=self.offset)
56 self.offset_entry.pack(side=tk.TOP)
57
58 tk.Button(button_frame,
59 text="Update",
60 width=self.default_button_width,
61 command=self.update_canvas).pack(side=tk.TOP, fill=tk.BOTH, expand=True)
62
63 tk.Button(
64 button_frame,
65 text="Load file",
66 width=self.default_button_width,
67 command=self.load_file).pack(side="top", fill="both", expand=True)
68
69 tk.Button(
70 button_frame,
71 text="Quit",
72 width=self.default_button_width,
73 command=self.quit).pack(side="top", fill="both", expand=True)
74
75 def current_bytes(self):
76 return b"".join([self.bytes[each: each+self.canvas_size[0]] for each in range(self.offset.get(),
77 len(self.bytes),
78 self.cycle.get())])
79 def update_canvas(self):
80 self.image = ImageTk.PhotoImage(Image.frombytes("L", self.canvas_size, self.current_bytes()))
81 self.canvas.itemconfig(self.image_id, image=self.image)
82
83 def load_file(self):
84 pass
85
86 def quit(self):
87 self.parent.destroy()
88
89
90if __name__ == '__main__':
91 root = tk.Tk()
92 root.geometry("1920x1200")
93
94 app = MainApplication(root)
95
96 root.mainloop()
Note: See TracBrowser for help on using the repository browser.