250x250
    
    
  
														Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
													Tags
													
											
												
												- Python
 - Tkinter
 - kubernetes
 - 논문 리뷰
 - tensorflow
 - vue.js
 - POD
 - 컴퓨터 비전
 - Docker
 - MariaDB
 - 웹 프로그래밍
 - 그래픽 유저 인터페이스
 - OpenCV
 - GUI
 - Web Programming
 - 장고
 - yaml
 - Deep Learning
 - Django
 - 파이썬
 - 텐서플로우
 - numpy
 - 딥러닝
 - FLASK
 - pytorch
 - 파이토치
 - k8s
 - paper review
 - 데이터베이스
 - Computer Vision
 
													Archives
													
											
												
												- Today
 
- Total
 
Maxima's Lab
[Python, Tkinter] 지정된 경로에 이미지 실시간 감지(Detect), 로드(Load) 및 시각화(Visualize) 본문
			Python/GUI (Graphic User Interface)
			
		[Python, Tkinter] 지정된 경로에 이미지 실시간 감지(Detect), 로드(Load) 및 시각화(Visualize)
Minima 2023. 2. 26. 22:12728x90
    
    
  SMALL
    안녕하세요, 오늘은 Tkinter 모듈을 사용하여, 지정된 경로에 이미지를 실시간으로 감지(Detect), 로드(Load)하고 시각화(Visualize)하는 방법에 대해서 알아보도록 하겠습니다.
위의 방법에 대한 전체 코드는 다음과 같습니다.
import os
import tkinter as tk
from PIL import Image, ImageTk
class ImageLoader(tk.Frame):
    def __init__(self, master=None, path=None):
        super().__init__(master)
        self.master = master
        self.path = path
        self.image = None
        self.label = tk.Label(self.master)
        self.label.pack()
        self.load_image()
    def load_image(self):
        if os.path.exists(self.path):
            try:
                image = Image.open(self.path)
                self.image = ImageTk.PhotoImage(image)
                self.label.config(image=self.image)
                
                print("Image Detected")
            except Exception as e:
                print(f"Error loading image: {e}")
        self.master.after(1000, self.load_image)
if __name__ == "__main__":
    root = tk.Tk()
    root.title("Maxima")
    root.geometry("1000x800+200+200")
    
    img_loader = ImageLoader(root, "./image.png")
    img_loader.pack()
    root.mainloop()
현재 경로에 "image.png" 라는 파일명을 가진 이미지가 존재하는 지 여부를 실시간으로 감지하고 로드하는 코드입니다. 실시간으로 감시하는 주기는 1초라는 시간 간격으로 monitoring을 하여 이미지가 감지하게 구성하였습니다.
추가적으로 위의 코드 내 load_image 함수에 대해서 알아보도록 하겠습니다.
    def load_image(self):
        if os.path.exists(self.path):
            try:
                image = Image.open(self.path)
                self.image = ImageTk.PhotoImage(image)
                self.label.config(image=self.image)
                
                print("Image Detected")
            except Exception as e:
                print(f"Error loading image: {e}")
        self.master.after(1000, self.load_image)
load_image 함수에서 주어진 경로의 이미지를 입력 받아 존재 여부를 확인하고 정상적으로 로드가 되었을 시 Load 후 Image를 tk.Label을 통해 시각화 합니다. 해당 이미지의 존재여부를 실시간으로 감지하는 시간 주기는 1000ms (1초)입니다.
아래 이미지는 정상적으로 이미지가 탐지되어 로드 후 시각화 된 결과입니다.

이상으로, 지정된 경로에 이미지를 실시간으로 감지하고 로드 후 시각화하는 방법에 대해서 알아보았습니다.
728x90
    
    
  LIST
    'Python > GUI (Graphic User Interface)' 카테고리의 다른 글
			  Comments