tkinter GUI
OpenCV 2018. 12. 29. 10:06 |반응형
OpenCV와 함께 tkinter를 이용해 아래처럼 GUI를 원하는대로 만들 수 있다.
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 | import cv2 import sys import tkinter as tk from PIL import Image, ImageTk # The ImageTk module contains support to create and modify Tkinter BitmapImage and PhotoImage objects from PIL images main = tk.Tk() main.title("OpenCV-tkinter") cvFrame = tk.Frame(main) # A frame is basically just a container for other widgets. cvFrame.grid(row = 0, column = 0, padx = 10, pady = 10) lbl1 = tk.Label(cvFrame) lbl1.grid(row = 0, column = 0) lbl2 = tk.Label(cvFrame) lbl2.grid(row = 0, column = 1) def ExitButton(): sys.exit() btn = tk.Button(cvFrame, text = "Exit", font = ('Arial', '30', 'bold'), foreground = "Red", command = ExitButton) # (.., height = 2, width = 60, ..) # fron - As a tuple whose first element is the font family, followed by a size (in points if positive, in pixels if negative), optionally followed by # a string containing one or more of the style modifiers bold, italic, underline, and overstrike. btn.grid(row = 1, column = 0, columnspan = 2, sticky = tk.N + tk.S + tk.W + tk.E) # columnspan - Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and merge them into one # larger cell by setting the columnspan option to the number of cells. For example, w.grid(row=0, column=2, columnspan=3) would place widget # w in a cell that spans columns 2, 3, and 4 of row 0. # sticky - This option determines how to distribute any extra space within the cell that is not taken up by the widget at its natural size. cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) print('frame_size =', frame_size) def show_frame(): retval, frame = cap.read() frame = cv2.flip(frame, 1) cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) img = Image.fromarray(cv2image) # Creates an image memory from an object exporting the array interface (using the buffer protocol). imgtk = ImageTk.PhotoImage(image = img) # A Tkinter-compatible photo image. This can be used everywhere Tkinter expects an image object. # If the image is an RGBA image, pixels having alpha 0 are treated as transparent. lbl1.imgtk = imgtk lbl1.configure(image = imgtk) # Set the values of one or more options. lbl2.imgtk = imgtk lbl2.configure(image = imgtk) # lbl.after(10, show_frame) main.after(10, show_frame) # Requests Tkinter to call function callback with arguments args after a delay of at least delay_ms milliseconds. show_frame() main.mainloop() | cs |
반응형
'OpenCV' 카테고리의 다른 글
OpenCV Graphic User Interface(GUI) with cvui (1) | 2019.07.01 |
---|---|
How to extract video from a video file 영상 추출 하기 (0) | 2019.06.29 |
Video Analysis - Optical Flow (0) | 2019.01.03 |
Video Analysis - Meanshift and CAMshift (0) | 2018.12.27 |
Wafer detection by Hough transform - OpenCV 반도체 웨이퍼 감지 (0) | 2018.12.24 |