-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (64 loc) · 1.99 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
import pyaudio
import tkinter as tk
import numpy as np
import time
from PIL import Image, ImageDraw, ImageOps, ImageTk
# convert image to circle
def image_to_circle(image_path):
im = Image.open(image_path)
# create mask
mask = Image.new('L', im.size)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + mask.size, fill = 255)
output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('out.png')
image_to_circle('test.png')
# setup stream
pa = pyaudio.PyAudio()
stream = pa.open(
rate = 44100,
frames_per_buffer = 1024,
input = True,
channels = 1,
format = pyaudio.paInt16
)
# create the window
root = tk.Tk()
root.geometry('600x400')
# add canvas
canvas = tk.Canvas(root, background = 'black', highlightthickness=0)
canvas.pack(fill='both', expand=True)
def draw(audio_data):
canvas.delete('all')
max_circle_size = 200 + (np.max(audio_data) - np.min(audio_data))
if max_circle_size < 220:
max_circle_size = 200
circle_x = (root.winfo_width() - max_circle_size) / 2
circle_y = (root.winfo_height() - max_circle_size) / 2
end_x = circle_x + max_circle_size
end_y = circle_y + max_circle_size
image_x = (root.winfo_width() / 2)
image_y = (root.winfo_height() / 2)
image = ImageTk.PhotoImage(Image.open('out.png').resize((
200,
200
)))
root.image = image
# draw circle
time.sleep(0.005)
canvas.create_oval(circle_x, circle_y, end_x, end_y, fill = '#0984e3')
# draw image in the center of circle
canvas.create_image((image_x, image_y), image = image)
while True:
try:
audio_data = np.frombuffer(stream.read(1024), dtype = np.int16)
draw(audio_data)
root.update_idletasks()
root.update()
except tk.TclError as e:
print(e)
break
stream.stop_stream()
stream.close()
#root.mainloop()