Last active
August 7, 2024 16:46
-
-
Save simonjenny/b84946d4de1698ebe8acb7775414015f to your computer and use it in GitHub Desktop.
Slideshow for Raspberry Pi (or any other Python capable OS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os, pygame, random | |
pygame.init() | |
pygame.mouse.set_visible(False) | |
BACKGROUND = (0,0,0) | |
INTERVAL = os.getenv('INTERVAL', 60) | |
IMAGEFOLDER = os.getenv('IMAGEFOLDER', 'images') | |
X = pygame.display.Info().current_w | |
Y = pygame.display.Info().current_h | |
size = (X, Y) | |
display = pygame.display.set_mode(size) | |
def scaleImage(img,(bx,by)): | |
ix,iy = img.get_size() | |
if ix > iy: | |
scale_factor = bx/float(ix) | |
sy = scale_factor * iy | |
if sy > by: | |
scale_factor = by/float(iy) | |
sx = scale_factor * ix | |
sy = by | |
else: | |
sx = bx | |
else: | |
scale_factor = by/float(iy) | |
sx = scale_factor * ix | |
if sx > bx: | |
scale_factor = bx/float(ix) | |
sx = bx | |
sy = scale_factor * iy | |
else: | |
sy = by | |
return pygame.transform.scale(img, (int(sx),int(sy))) | |
def fade(image, direction): | |
for i in direction: | |
image.set_alpha(i) | |
display.fill(BACKGROUND) | |
display.blit(image, ( (0.5 * X) - (0.5 * image.get_width() ), (0.5 * Y) - (0.5 * image.get_height() ) )) | |
pygame.display.flip() | |
def printLine(fontsize, text, position, background, foreground): | |
font = pygame.font.SysFont ( "", fontsize ) | |
line = font.render(text, True, background, foreground) | |
lineRect = line.get_rect() | |
lineRect.center = position | |
display.blit(line, lineRect) | |
### Preload Images from Image Directory and show progress | |
files = os.listdir(IMAGEFOLDER) | |
process = 0 | |
cache = [] | |
for i in files: | |
if os.path.isfile(IMAGEFOLDER + "/" + i) and not i.endswith('.HEIC'): | |
img = scaleImage(pygame.image.load(IMAGEFOLDER + "/" + i).convert(), size) | |
cache.append(img) | |
process += 1 | |
bar = 80 + ((X - 80) // len(files)) * process | |
display.fill((0,255,0), pygame.Rect((X // 2) - (bar // 2) , (Y // 2) - (25 // 2) , bar ,25)) | |
printLine(20, "Loading...", ((X // 2), (Y // 2)), (0,0,0), (0,255,0)) | |
pygame.display.flip() | |
pygame.time.delay(1000) | |
while True: | |
display.fill(BACKGROUND) | |
image = random.choice(cache) | |
fade(image, range(50,255,3)) | |
pygame.time.delay(int(INTERVAL) * 1000) | |
fade(image, range(255,50,-3)) | |
for event in pygame.event.get() : | |
if event.type == pygame.QUIT : | |
pygame.quit() | |
quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires : PyGame
Run with : python slideshow.py
Optional : INTERVAL=30 IMAGEFOLDER=/home/pi/pictures python slideshow.py