Skip to content

Instantly share code, notes, and snippets.

@villares
Last active July 26, 2024 23:25
Show Gist options
  • Save villares/4ae0921fdef5a01b7cd27708d0c89eef to your computer and use it in GitHub Desktop.
Save villares/4ae0921fdef5a01b7cd27708d0c89eef to your computer and use it in GitHub Desktop.
Show images or SVGs from a folder named 'data' next to your py5 imported mode script.
"""
You'll need py5 and a use the run_sketch tool or Thonny + thonny-py5mode
to run this py5 "imported mode" style sketch. Learn more at py5coding.org
Add images or SVGs to a folder named 'data' next to your animated image show script.
It will run on full screen. Click on screen and press ESC to exit.
Adjust speed by changing the time_per_image variable in milliseconds.
Beware py5 might be unable to load some SVGs... an error message will be printed.
This version will not try to scale images to fit the screen.
"""
from pathlib import Path
# create a folder called 'data' next to your screensaver.py file
data_folder = Path.cwd() / 'data'
imgs_and_svgs = [] # list of images/svgs
time_per_image = 3000 # milliseconds
last_image_time = 0
index_current_image = 0
def setup():
full_screen() # this is an option insteado of size()
image_mode(CENTER)
shape_mode(CENTER)
for path in data_folder.iterdir(): # walk the data_folder
try:
if path.suffix.lower() in ('.png', '.jpg', '.jpeg'):
img = load_image(path)
imgs_and_svgs.append((img, 'image'))
elif path.suffix.lower() == '.svg':
shp = load_shape(path)
imgs_and_svgs.append((shp, 'svg'))
except Exception as err:
print(f'image {path.name} caused error:\n{err}')
def draw():
global index_current_image
global last_image_time
background(0)
obj, kind = imgs_and_svgs[index_current_image]
if kind == 'svg':
shape(obj, width/2, height/2)
else:
image(obj, width/2, height/2)
if millis() - last_image_time > time_per_image:
number_imgs = len(imgs_and_svgs)
index_current_image = (index_current_image + 1) % number_imgs
last_image_time = millis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment