Nedan presenteras min spaghettikod i python för att precentera bilder som ligger i /boot/foto/ på en hdmi skärm.
import sys, os
if sys.version_info[0] == 2:
import Tkinter
tkinter = Tkinter
else:
import tkinter
from PIL import Image, ImageTk
import time
import datetime
import touchphat
touchphat.all_off()
if os.environ.get('DISPLAY','') == '':
print('no display found. Using :0.0')
os.environ.__setitem__('DISPLAY', ':0.0')
root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
textid = 0
imageid = 0
add24h = 0
add1h = 0
add10m = 0
add1m = 0
then = 0
@touchphat.on_touch("A")
def handle_touch():
print("Button A pressed!")
global add24h
add24h = add24h + 1
@touchphat.on_touch("B")
def handle_touch():
print("Button B pressed!")
global add1h
add1h = add1h + 1
@touchphat.on_touch("C")
def handle_touch():
print("Button C pressed!")
global add10m
add10m = add10m + 1
@touchphat.on_touch("D")
def handle_touch():
print("Button D pressed!")
global add1m
add1m = add1m + 1
def showPIL(pilImage):
global textid
global imageid
global add24h
global add1h
global add10m
global add1m
if textid != 0:
print ("removing old picture")
canvas.delete(imageid)
else:
print ("not removing old picture, no old picture")
print ("Calculating sceen for photo")
imgWidth, imgHeight = pilImage.size
# resize photo to full screen
ratio = min(w/imgWidth, h/imgHeight)
imgWidth = int(imgWidth*ratio)
imgHeight = int(imgHeight*ratio)
pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image((w/2)+35,h/2,image=image)
imageid = imagesprite
print ("Sprite nr",imagesprite)
t0 = time.time() # now (in seconds)
t1 = t0 + 60*60*10*add24h # now + 12 houres
t1 = t1 + 60*60*add1h # now + 60 minutes
t1 = t1 + 60*10*add10m # now + 10 min
t1 = t1 + 60*add1m # now + 1 min
print ("Calculated time")
current_time = time.strftime("%H:%M",time.localtime(t1))
if textid != 0:
print ("removing old clock")
canvas.delete(textid)
else:
print ("not removing old time, no old time")
textid = canvas.create_text(w-180, h-40, font=("Purisa", 80), text=current_time, fill="red")
print ("updating screen")
root.update_idletasks()
root.update()
# root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
names = os.listdir("/boot/foto/")
i = 0
while i <= len(names):
now = time.time()
if now >= then+10:
if names[i][-4:] == ".jpg":
print(i)
print (len(names))
print (names[i])
file=Image.open("/boot/foto/"+names[i])
showPIL(file)
then = time.time()
i += 1
if i == len(names):
i = 0