source: ammosreader/sample_scripts/ammos_viewer.py@ 577ce87

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

use cycle from frame_size and adjust canvas

  • Property mode set to 100644
File size: 3.3 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 = self.ammos_container.unique_frame_sizes()[0]
22 self.cache = 500000000
23 self.canvas_size = (800, 600)
24 self.offset = 0
25 self.buildup()
26
27 def buildup(self):
28 """I build up the initial user interface."""
29 self.parent.title("File name:" + self.file_name + " Size:" + str(len(self.bytes)))
30 self.pack(fill="both", expand=True)
31 self.default_button_width = 10
32 image_frame = tk.Frame(self)
33 image_frame.pack(side=tk.LEFT)
34 self.canvas = tk.Canvas(image_frame, bg="#000000", width=self.canvas_size[0], height=self.canvas_size[1])
35 self.canvas.pack()
36 # display_size = self.canvas_size[0] * self.canvas_size[1]
37 # bytes_to_display = randbytes(display_size)
38 # if len(self.bytes) < display_size:
39 # bytes_to_display = self.bytes[self.offset:] + bytearray([0xff] * (display_size-len(self.bytes)-self.offset))
40 current_bytes = b"".join([self.bytes[each: each+self.canvas_size[0]] for each in range(0, len(self.bytes),
41 self.cycle)])
42 print("Bytes size", len(current_bytes))
43 pil_image = Image.frombytes("L", self.canvas_size, current_bytes)
44 self.image = ImageTk.PhotoImage(pil_image)
45 # pil_image.show()
46 button_frame = tk.Frame(self)
47 self.canvas.create_image(0, 0, anchor='nw', image=self.image)
48 button_frame.pack(side="right")
49
50 tk.Label(button_frame, text='Cycle in bits').pack(side=tk.TOP)
51 self.cycle_entry = tk.Entry(button_frame)
52 # self.cycle_entry.set()
53 self.cycle_entry.pack(side=tk.TOP)
54
55 tk.Label(button_frame, text='Cache in bytes').pack(side=tk.TOP)
56 self.cache_entry = tk.Entry(button_frame)
57 self.cache_entry.pack(side=tk.TOP)
58
59 tk.Label(button_frame, text='Offset in bits').pack(side=tk.TOP)
60 self.offset_entry = tk.Entry(button_frame)
61 self.offset_entry.pack(side=tk.TOP)
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 load_file(self):
76 pass
77
78 def quit(self):
79 self.parent.destroy()
80
81
82if __name__ == '__main__':
83 root = tk.Tk()
84 root.geometry("1920x1200")
85
86 app = MainApplication(root)
87
88 root.mainloop()
Note: See TracBrowser for help on using the repository browser.