Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
ec974a5697 | |||
84d7df9495 | |||
1c3db38c5e | |||
c375f925d1 |
258
main.py
258
main.py
@ -1,148 +1,184 @@
|
|||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
from PIL import Image
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
|
||||||
# from PIL import Image
|
|
||||||
import threading
|
import threading
|
||||||
# import exifread
|
from watchdog.observers import Observer
|
||||||
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
|
||||||
# Chemin du dossier contenant les photos
|
# Chemin vers le dossier contenant les images
|
||||||
PHOTO_FOLDER = "./photos"
|
IMAGE_DIR = './photos'
|
||||||
# Temps affichage photos (1000 = 1 seconde)
|
|
||||||
TIMER = 8000
|
|
||||||
|
|
||||||
|
|
||||||
# Liste des fichiers dans le dossier
|
|
||||||
def get_photo_files():
|
|
||||||
return [os.path.join(PHOTO_FOLDER, f) for f in os.listdir(PHOTO_FOLDER) if
|
|
||||||
os.path.isfile(os.path.join(PHOTO_FOLDER, f))]
|
|
||||||
|
|
||||||
|
|
||||||
photo_files = get_photo_files()
|
|
||||||
|
|
||||||
# Initialisation de Pygame
|
# Initialisation de Pygame
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
pygame.display.set_caption("Diaporama")
|
||||||
|
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
|
||||||
|
|
||||||
# Récupération de la résolution de l'écran
|
# Créez une nouvelle surface pour dessiner les images et les logos
|
||||||
info = pygame.display.Info()
|
intermediate_surface = pygame.Surface(screen.get_size())
|
||||||
max_width = info.current_w
|
|
||||||
max_height = info.current_h
|
|
||||||
|
|
||||||
# Création de la fenêtre
|
|
||||||
screen = pygame.display.set_mode((max_width, max_height), FULLSCREEN)
|
|
||||||
|
|
||||||
# Initialisation de la variable fullscreen
|
|
||||||
fullscreen = False
|
|
||||||
|
|
||||||
# Boucle principale
|
|
||||||
running = True
|
|
||||||
photo_index = 0
|
|
||||||
clock = pygame.time.Clock()
|
|
||||||
auto_change = True
|
|
||||||
auto_change_timer = 0
|
|
||||||
pause = False # Initialisation de la variable pause
|
|
||||||
|
|
||||||
|
|
||||||
# Chargement des photos
|
|
||||||
def load_photos():
|
|
||||||
global photo_files
|
|
||||||
global photo_index
|
|
||||||
while True:
|
|
||||||
if not auto_change:
|
|
||||||
continue
|
|
||||||
next_index = (photo_index + 1) % len(photo_files)
|
|
||||||
pygame.image.load(photo_files[next_index])
|
|
||||||
pygame.time.wait(10)
|
|
||||||
|
|
||||||
|
|
||||||
threading.Thread(target=load_photos, daemon=True).start()
|
|
||||||
|
|
||||||
# Logos Play, Pause, Next, Previous
|
# Logos Play, Pause, Next, Previous
|
||||||
LOGO_SIZE = (200, 200)
|
LOGO_SIZE = (200, 200)
|
||||||
|
|
||||||
pause_logo = pygame.image.load("./Logos/pause.png")
|
pause_logo = pygame.image.load("./logos/pause.png")
|
||||||
pause_logo = pygame.transform.smoothscale(pause_logo, LOGO_SIZE)
|
pause_logo = pygame.transform.smoothscale(pause_logo, LOGO_SIZE)
|
||||||
pause_logo_rect = pause_logo.get_rect(center=screen.get_rect().center)
|
pause_logo_rect = pause_logo.get_rect(center=screen.get_rect().center)
|
||||||
|
|
||||||
play_logo = pygame.image.load("./Logos/play.png")
|
play_logo = pygame.image.load("./logos/play.png")
|
||||||
play_logo = pygame.transform.smoothscale(play_logo, LOGO_SIZE)
|
play_logo = pygame.transform.smoothscale(play_logo, LOGO_SIZE)
|
||||||
play_logo_rect = play_logo.get_rect(center=screen.get_rect().center)
|
play_logo_rect = play_logo.get_rect(center=screen.get_rect().center)
|
||||||
|
|
||||||
next_logo = pygame.image.load("./Logos/next.png")
|
next_logo = pygame.image.load("./logos/next.png")
|
||||||
next_logo = pygame.transform.smoothscale(next_logo, LOGO_SIZE)
|
next_logo = pygame.transform.smoothscale(next_logo, LOGO_SIZE)
|
||||||
next_logo_rect = next_logo.get_rect(midright=(screen.get_rect().right - 50, screen.get_rect().centery))
|
next_logo_rect = next_logo.get_rect(midright=(screen.get_rect().right - 200, screen.get_rect().centery))
|
||||||
|
|
||||||
previous_logo = pygame.image.load("./Logos/previous.png")
|
previous_logo = pygame.image.load("./logos/previous.png")
|
||||||
previous_logo = pygame.transform.smoothscale(previous_logo, LOGO_SIZE)
|
previous_logo = pygame.transform.smoothscale(previous_logo, LOGO_SIZE)
|
||||||
previous_logo_rect = previous_logo.get_rect(midleft=(screen.get_rect().left + 50, screen.get_rect().centery))
|
previous_logo_rect = previous_logo.get_rect(midleft=(screen.get_rect().left + 200, screen.get_rect().centery))
|
||||||
|
|
||||||
|
# Chargement des images dans une liste
|
||||||
|
images = []
|
||||||
|
|
||||||
|
|
||||||
|
# Fonction pour charger une image en utilisant un thread
|
||||||
|
def load_image(local_image_path):
|
||||||
|
with Image.open(local_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):
|
||||||
|
if file_name.endswith('.jpg') or file_name.endswith('.png'):
|
||||||
|
image_path = os.path.join(IMAGE_DIR, file_name)
|
||||||
|
image_thread = threading.Thread(target=load_image, args=(image_path,))
|
||||||
|
image_threads.append(image_thread)
|
||||||
|
image_thread.start()
|
||||||
|
|
||||||
|
# Attente de la fin du chargement de toutes les images
|
||||||
|
for image_thread in image_threads:
|
||||||
|
image_thread.join()
|
||||||
|
|
||||||
|
|
||||||
|
class ImageHandler(FileSystemEventHandler):
|
||||||
|
def on_modified(self, local_event):
|
||||||
|
if local_event.src_path.endswith('.jpg') or local_event.src_path.endswith('.png'):
|
||||||
|
load_image(local_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)
|
||||||
|
center_rect = pygame.Rect(screen_width // 3, 0, screen_width // 3, screen_height)
|
||||||
|
right_rect = pygame.Rect(screen_width * 2 // 3, 0, screen_width // 3, screen_height)
|
||||||
|
|
||||||
|
# Variables pour le diaporama
|
||||||
|
current_image_index = 0
|
||||||
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
is_paused = False
|
||||||
|
|
||||||
|
image = images[current_image_index]
|
||||||
|
image_rect = image.get_rect(center=screen.get_rect().center)
|
||||||
|
|
||||||
|
# Boucle principale
|
||||||
|
running = True
|
||||||
while running:
|
while running:
|
||||||
# Gestion des événements
|
# Gestion des événements Pygame
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
elif event.type == KEYDOWN and event.key == K_SPACE:
|
elif event.type == pygame.KEYDOWN:
|
||||||
auto_change = not auto_change
|
if event.key == pygame.K_ESCAPE:
|
||||||
if not auto_change:
|
running = False
|
||||||
screen.blit(pause_logo, pause_logo_rect)
|
elif event.key == pygame.K_LEFT:
|
||||||
|
current_image_index = (current_image_index - 1) % len(images)
|
||||||
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
screen.blit(previous_logo, previous_logo_rect)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
pygame.time.delay(1000) # Pause de 1 secondes 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))
|
||||||
else:
|
elif event.key == pygame.K_RIGHT:
|
||||||
screen.blit(play_logo, play_logo_rect)
|
screen.blit(next_logo, next_logo_rect)
|
||||||
|
current_image_index = (current_image_index + 1) % len(images)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
pygame.time.delay(1000) # Pause de 1 secondes 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))
|
||||||
elif event.type == KEYDOWN and event.key == K_LEFT:
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
photo_index = (photo_index - 1) % len(photo_files)
|
elif event.key == pygame.K_SPACE:
|
||||||
screen.blit(previous_logo, previous_logo_rect)
|
is_paused = not is_paused
|
||||||
pygame.display.flip()
|
if is_paused:
|
||||||
pygame.time.delay(1000) # Pause de 1 secondes avant de masquer le logo
|
screen.blit(pause_logo, pause_logo_rect)
|
||||||
screen.fill((0, 0, 0))
|
pygame.display.flip()
|
||||||
elif event.type == KEYDOWN and event.key == K_RIGHT:
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
photo_index = (photo_index + 1) % len(photo_files)
|
screen.fill((0, 0, 0))
|
||||||
screen.blit(next_logo, next_logo_rect)
|
else:
|
||||||
pygame.display.flip()
|
screen.blit(play_logo, play_logo_rect)
|
||||||
pygame.time.delay(1000) # Pause de 1 secondes avant de masquer le logo
|
pygame.display.flip()
|
||||||
screen.fill((0, 0, 0))
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
elif event.type == KEYDOWN and event.key == K_f:
|
screen.fill((0, 0, 0))
|
||||||
fullscreen = not fullscreen
|
elif event.type == pygame.MOUSEBUTTONUP:
|
||||||
if fullscreen:
|
pos = pygame.mouse.get_pos()
|
||||||
screen = pygame.display.set_mode((0, 0), FULLSCREEN)
|
if left_rect.collidepoint(pos):
|
||||||
else:
|
current_image_index = (current_image_index - 1) % len(images)
|
||||||
screen = pygame.display.set_mode((800, 600), RESIZABLE)
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
elif event.type == KEYDOWN and event.key == K_r:
|
screen.blit(previous_logo, previous_logo_rect)
|
||||||
photo_files = get_photo_files()
|
pygame.display.flip()
|
||||||
photo_index = 0
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
previous_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
elif center_rect.collidepoint(pos):
|
||||||
|
is_paused = not is_paused
|
||||||
|
if is_paused:
|
||||||
|
screen.blit(pause_logo, pause_logo_rect)
|
||||||
|
pygame.display.flip()
|
||||||
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
else:
|
||||||
|
screen.blit(play_logo, play_logo_rect)
|
||||||
|
pygame.display.flip()
|
||||||
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
elif right_rect.collidepoint(pos):
|
||||||
|
screen.blit(next_logo, next_logo_rect)
|
||||||
|
current_image_index = (current_image_index + 1) % len(images)
|
||||||
|
pygame.display.flip()
|
||||||
|
pygame.time.delay(1000) # Pause de 1 seconde avant de masquer le logo
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
|
||||||
# Changement automatique de photo
|
image = images[current_image_index]
|
||||||
if auto_change:
|
image_rect = image.get_rect(center=screen.get_rect().center)
|
||||||
if pygame.time.get_ticks() - auto_change_timer > TIMER:
|
|
||||||
photo_index = (photo_index + 1) % len(photo_files)
|
|
||||||
auto_change_timer = pygame.time.get_ticks()
|
|
||||||
|
|
||||||
# Affichage de la photo courante
|
# Dessinez l'image actuelle sur la surface intermédiaire
|
||||||
photo = pygame.image.load(photo_files[photo_index])
|
intermediate_surface.fill((0, 0, 0))
|
||||||
photo_rect = photo.get_rect()
|
intermediate_surface.blit(image, image_rect)
|
||||||
|
|
||||||
# Calcul de l'échelle nécessaire pour adapter la photo à la fenêtre
|
|
||||||
scale = min(float(screen.get_width()) / photo_rect.width, float(screen.get_height()) / photo_rect.height)
|
|
||||||
|
|
||||||
# Redimensionnement de la photo en conservant son rapport d'aspect
|
|
||||||
new_width = int(photo_rect.width * scale)
|
|
||||||
new_height = int(photo_rect.height * scale)
|
|
||||||
photo = pygame.transform.smoothscale(photo, (new_width, new_height))
|
|
||||||
|
|
||||||
# Positionnement de la photo au centre de la fenêtre
|
|
||||||
photo_rect = photo.get_rect(center=screen.get_rect().center)
|
|
||||||
|
|
||||||
# Affichage de la photo
|
|
||||||
screen.fill((0, 0, 0))
|
|
||||||
screen.blit(photo, photo_rect)
|
|
||||||
|
|
||||||
|
# Dessinez la surface intermédiaire sur l'écran
|
||||||
|
screen.blit(intermediate_surface, (0, 0))
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
# Attente avant de passer à la photo suivante
|
if not is_paused and pygame.time.get_ticks() >= next_image_time:
|
||||||
# clock.tick(60) # 60 images par seconde
|
current_image_index = (current_image_index + 1) % len(images)
|
||||||
|
next_image_time = pygame.time.get_ticks() + 5000
|
||||||
|
|
||||||
# Fermeture de Pygame
|
# Fermeture de Pygame
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
BIN
photos/matthew-buchanan-g-0dp2ycCw0.jpg
Normal file
BIN
photos/matthew-buchanan-g-0dp2ycCw0.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.0 MiB |
BIN
photos/neil-nourigat-tT3CGmnR1pk.jpg
Normal file
BIN
photos/neil-nourigat-tT3CGmnR1pk.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pygame~=2.3.0
|
||||||
|
Pillow~=9.5.0
|
||||||
|
watchdog~=3.0.0
|
Loading…
x
Reference in New Issue
Block a user