import tkinter as tk
from tkinter import PhotoImage
from PIL import Image, ImageTk
import os
import random
import time
class DigitalPhotoFrame(tk.Tk):
def __init__(self, image_folder, interval=5):
super().__init__()
self.image_folder = image_folder
self.image_files = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
self.interval = interval
self.label = tk.Label(self)
self.label.pack(pady=5, padx=5, expand=tk.YES, fill=tk.BOTH)
# 키 이벤트 연결
self.bind('<Key>', self.exit_program)
self.update_image()
def update_image(self):
image_path = random.choice(self.image_files)
image = Image.open(image_path)
image = self.resize_image(image, self.winfo_screenwidth(), self.winfo_screenheight())
self.tk_image = ImageTk.PhotoImage(image)
self.label.config(image=self.tk_image)
# 일정 시간마다 이미지 변경
self.after(self.interval * 1000, self.update_image)
def resize_image(self, img, max_width, max_height):
width, height = img.size
ratio = min(max_width / width, max_height / height)
new_size = (int(width*ratio), int(height*ratio))
return img.resize(new_size, Image.ANTIALIAS)
# 종료 함수 추가
def exit_program(self, event):
self.destroy()
if __name__ == "__main__":
image_folder_path = "/home/pi/pictures"
frame_interval = 5 # 이미지가 5초마다 변경됩니다.
app = DigitalPhotoFrame(image_folder_path, frame_interval)
app.attributes('-fullscreen', True)
app.mainloop()
Copyright ⓒ TECH79 All right reserved