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:
parent
c375f925d1
commit
1c3db38c5e
60
main.py
60
main.py
@ -1,16 +1,19 @@
|
|||||||
import os
|
import os
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import pygame
|
import pygame
|
||||||
|
import threading
|
||||||
|
|
||||||
# Chemin vers le dossier contenant les images
|
# Chemin vers le dossier contenant les images
|
||||||
IMAGE_DIR = './photos'
|
IMAGE_DIR = './photos'
|
||||||
|
|
||||||
|
|
||||||
# Initialisation de Pygame
|
# Initialisation de Pygame
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption("Diaporama")
|
pygame.display.set_caption("Diaporama")
|
||||||
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
|
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
|
||||||
|
|
||||||
|
# Créez une nouvelle surface pour dessiner les images et les logos
|
||||||
|
intermediate_surface = pygame.Surface(screen.get_size())
|
||||||
|
|
||||||
# Logos Play, Pause, Next, Previous
|
# Logos Play, Pause, Next, Previous
|
||||||
LOGO_SIZE = (200, 200)
|
LOGO_SIZE = (200, 200)
|
||||||
|
|
||||||
@ -32,13 +35,25 @@ previous_logo_rect = previous_logo.get_rect(midleft=(screen.get_rect().left + 20
|
|||||||
|
|
||||||
# Chargement des images dans une liste
|
# Chargement des images dans une liste
|
||||||
images = []
|
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):
|
for file_name in os.listdir(IMAGE_DIR):
|
||||||
if file_name.endswith('.jpg') or file_name.endswith('.png'):
|
if file_name.endswith('.jpg') or file_name.endswith('.png'):
|
||||||
image_path = os.path.join(IMAGE_DIR, file_name)
|
image_path = os.path.join(IMAGE_DIR, file_name)
|
||||||
with Image.open(image_path) as img:
|
image_thread = threading.Thread(target=load_image, args=(image_path,))
|
||||||
# Redimensionnement de l'image en conservant le ratio d'aspect
|
image_threads.append(image_thread)
|
||||||
img.thumbnail(screen.get_size())
|
image_thread.start()
|
||||||
images.append(pygame.image.fromstring(img.tobytes(), img.size, "RGB"))
|
|
||||||
|
# Attente de la fin du chargement de toutes les images
|
||||||
|
for image_thread in image_threads:
|
||||||
|
image_thread.join()
|
||||||
|
|
||||||
# Définition des zones d'affichage
|
# Définition des zones d'affichage
|
||||||
screen_width, screen_height = screen.get_size()
|
screen_width, screen_height = screen.get_size()
|
||||||
@ -51,6 +66,9 @@ current_image_index = 0
|
|||||||
next_image_time = pygame.time.get_ticks() + 5000
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
is_paused = False
|
is_paused = False
|
||||||
|
|
||||||
|
image = images[current_image_index]
|
||||||
|
image_rect = image.get_rect(center=screen.get_rect().center)
|
||||||
|
|
||||||
# Boucle principale
|
# Boucle principale
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
@ -68,7 +86,6 @@ while running:
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
screen.fill((0, 0, 0))
|
screen.fill((0, 0, 0))
|
||||||
previous_image_time = pygame.time.get_ticks() + 5000
|
|
||||||
elif event.key == pygame.K_RIGHT:
|
elif event.key == pygame.K_RIGHT:
|
||||||
screen.blit(next_logo, next_logo_rect)
|
screen.blit(next_logo, next_logo_rect)
|
||||||
current_image_index = (current_image_index + 1) % len(images)
|
current_image_index = (current_image_index + 1) % len(images)
|
||||||
@ -118,29 +135,20 @@ while running:
|
|||||||
screen.fill((0, 0, 0))
|
screen.fill((0, 0, 0))
|
||||||
next_image_time = pygame.time.get_ticks() + 5000
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
|
||||||
# Affichage de l'image actuelle
|
image = images[current_image_index]
|
||||||
|
image_rect = image.get_rect(center=screen.get_rect().center)
|
||||||
|
|
||||||
|
# Dessinez l'image actuelle sur la surface intermédiaire
|
||||||
|
intermediate_surface.fill((0, 0, 0))
|
||||||
|
intermediate_surface.blit(image, image_rect)
|
||||||
|
|
||||||
|
# Dessinez la surface intermédiaire sur l'écran
|
||||||
|
screen.blit(intermediate_surface, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
if not is_paused and pygame.time.get_ticks() >= next_image_time:
|
if not is_paused and pygame.time.get_ticks() >= next_image_time:
|
||||||
current_image_index = (current_image_index + 1) % len(images)
|
current_image_index = (current_image_index + 1) % len(images)
|
||||||
next_image_time = pygame.time.get_ticks() + 5000
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
|
||||||
image = images[current_image_index]
|
|
||||||
image_rect = image.get_rect(center=screen.get_rect().center)
|
|
||||||
|
|
||||||
screen.fill((0, 0, 0))
|
|
||||||
screen.blit(image, image_rect)
|
|
||||||
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
# Gestion des logos
|
|
||||||
if pygame.time.get_ticks() < next_image_time:
|
|
||||||
if not is_paused:
|
|
||||||
screen.blit(play_logo, play_logo_rect)
|
|
||||||
else:
|
|
||||||
screen.blit(pause_logo, pause_logo_rect)
|
|
||||||
pygame.time.wait(1000)
|
|
||||||
screen.fill((0, 0, 0))
|
|
||||||
screen.blit(image, image_rect)
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
# Fermeture de Pygame
|
# Fermeture de Pygame
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user