Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import tkinter as tk
  2. import cv2
  3. import PIL.Image, PIL.ImageTk
  4. import time
  5.  
  6. class MainWindow(tk.Frame):
  7. def __init__(self, *args, **kwargs):
  8.  
  9. tk.Frame.__init__(self, *args, **kwargs)
  10.  
  11. self.canvas = tk.Canvas(self, width=720, height=480)
  12. self.canvas.pack()
  13.  
  14. self.vid = cv2.VideoCapture(0)
  15.  
  16. self.btn_snapshot = tk.Button(self, text="Take photo", width=20, command=self.snapshot)
  17. self.btn_snapshot.pack(anchor=tk.CENTER, expand=True)
  18.  
  19. self.Slider_1 = tk.Scale(self, length=300.0, from_=0, to=200, orient=tk.HORIZONTAL)
  20. self.Slider_1.pack()
  21.  
  22. self.button = tk.Button(self, text="Create new window",
  23. command=self.create_window)
  24. self.button.pack(side="top")
  25.  
  26. self.show_frame()
  27.  
  28. def show_frame(self):
  29. _, frame = self.vid.read()
  30. frame = cv2.flip(frame, 1)
  31. cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
  32. self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2image))
  33. self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
  34. tk.Label(root).after(10, self.show_frame)
  35.  
  36.  
  37. def create_window(self):
  38. tl = tk.Toplevel(self)
  39. tl.wm_title("Window new")
  40. l = tk.Label(tl, text="This is window")
  41. l.focus_force()
  42. l.pack(side="top", fill="both", expand=True, padx=100, pady=100)
  43.  
  44. def snapshot(self):
  45. pass
  46. ret, frame = self.vid.get_frame() #will be soon
  47. if ret:
  48. cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
  49.  
  50. if __name__ == "__main__":
  51. root = tk.Tk()
  52. main = MainWindow(root)
  53. main.pack(side="top", fill="both", expand=True)
  54. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement