Amélioration des performances en chargeant les photos en dehors de la boucle principale via l'utilisation d'un thread. Utilisation d'Surface intermédiaire pour éviter de remplir l'écran de noir à chaque itération.

This commit is contained in:
Guillaume Archambault 2023-04-19 12:21:46 +02:00
parent 1c3db38c5e
commit 84d7df9495
3 changed files with 29 additions and 0 deletions

29
main.py
View File

@ -1,7 +1,10 @@
import os
import time
from PIL import Image
import pygame
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Chemin vers le dossier contenant les images
IMAGE_DIR = './photos'
@ -36,12 +39,14 @@ previous_logo_rect = previous_logo.get_rect(midleft=(screen.get_rect().left + 20
# Chargement des images dans une liste
images = []
# Fonction pour charger une image en utilisant un thread
def load_image(image_path):
with Image.open(image_path) as img:
img.thumbnail(screen.get_size())
images.append(pygame.image.fromstring(img.tobytes(), img.size, "RGB"))
# Création des threads pour charger les images
image_threads = []
for file_name in os.listdir(IMAGE_DIR):
@ -55,6 +60,30 @@ for file_name in os.listdir(IMAGE_DIR):
for image_thread in image_threads:
image_thread.join()
class ImageHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path.endswith('.jpg') or event.src_path.endswith('.png'):
load_image(event.src_path)
def watch_images_directory():
event_handler = ImageHandler()
observer = Observer()
observer.schedule(event_handler, IMAGE_DIR, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
watch_thread = threading.Thread(target=watch_images_directory)
watch_thread.daemon = True
watch_thread.start()
# Définition des zones d'affichage
screen_width, screen_height = screen.get_size()
left_rect = pygame.Rect(0, 0, screen_width // 3, screen_height)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB