Created
May 13, 2019 13:21
-
-
Save MikeTheWatchGuy/270e82809086729a07ab460c84acc522 to your computer and use it in GitHub Desktop.
Displays images from a folder on a rotation basis
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
#!/usr/bin/env python | |
import PySimpleGUI as sg | |
import os | |
from sys import exit as exit | |
from PIL import Image | |
import io | |
# Helper func | |
def image_file_to_bytes(filename, size): | |
try: | |
image = Image.open(filename) | |
image.thumbnail(size, Image.ANTIALIAS) | |
bio = io.BytesIO() # a binary memory resident stream | |
image.save(bio, format='PNG') # save image as png to it | |
imgbytes = bio.getvalue() | |
except: | |
imgbytes = None | |
return imgbytes | |
# Get the folder containing the images from the user | |
folder = sg.PopupGetFolder('Image folder to open') | |
if folder is None: | |
sg.PopupCancel('Cancelling') | |
exit(0) | |
# get list of PNG files in folder | |
png_files = [os.path.join(folder, f) for f in os.listdir(folder) if '.png' in f] | |
filenames_only = [f for f in os.listdir(folder) if '.png' in f] | |
if len(png_files) == 0: | |
sg.Popup('No PNG images in folder') | |
exit(0) | |
# -------------------- BEGIN GUI SECTION -------------------- | |
layout = [[sg.Graph((500,500), (0,0), (500,500), background_color='black', key='_GRAPH_', enable_events=True)], | |
[sg.B('Next'), sg.B('Exit')]] | |
window = sg.Window('Image Browser', | |
layout, | |
no_titlebar=True, | |
grab_anywhere=True, | |
background_color='black', | |
use_default_focus=False ) | |
# -------========= Event Loop =========-------- | |
display_index=0 | |
prev_name = None | |
while True: | |
event, values = window.Read(timeout=1000) | |
if event in (None, 'Exit'): break | |
cur_filename = png_files[display_index] | |
next_index = display_index + 1 if display_index+1 < len(png_files) else 0 | |
next_name = png_files[next_index] | |
if prev_name: | |
imgbytes = image_file_to_bytes(filename=prev_name, size=(200,200)) # draw previous file | |
window.Element('_GRAPH_').DrawImage(data=imgbytes, location=(325,300)) | |
if next_name: | |
imgbytes = image_file_to_bytes(filename=next_name, size=(225,200)) # draw previous file | |
window.Element('_GRAPH_').DrawImage(data=imgbytes, location=(0,300)) | |
imgbytes = image_file_to_bytes(filename=cur_filename, size=(500,500)) # draw current filename | |
window.Element('_GRAPH_').DrawImage(data=imgbytes, location=(125,400)) | |
display_index = display_index + 1 if display_index+1 < len(png_files) else 0 | |
prev_name = cur_filename | |
window.Close() |
Author
MikeTheWatchGuy
commented
May 13, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment