Code: Select all
import RPi.GPIO as GPIO
import os as sysCmd
import Tkinter as tk
from Tkinter import *
import ttk
import time
import picamera
import datetime
from PIL import Image
## GPIO settings
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.output(10, GPIO.HIGH)
## Call picamera and settings
camera = picamera.PiCamera()
camera.hflip = True
camera.vflip = True
camera.resolution = (800, 480)
debounce = .25
delay = 3
## Physical button switch to exit out of program but leave RPi on
def ButtonSwitch():
while GPIO.input(12) == 0:
time.sleep(0.25) ## Debounce
start_time = time.time()
while GPIO.input(12) == 0:
if time.time() - start_time > delay - debounce:
print("Button was Pressed")
#sysCmd.system('sudo killall idle')
self.quit()
root.after(3000, ButtonSwitch)
## Initiate the GUI and widgets
class MainScreen(Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.createWidget()
## Creating buttons
def createWidget(self):
self.pack()
bttn1 = tk.Button(self, text = "LEDS" , command = self.LEDs).grid (row = 1, column = 1, pady = 5, padx = 5)
bttn2 = tk.Button(self, text = "Quit" , command = self.quit).grid (row = 2, column = 1, pady = 5, padx = 5)
bttn3 = tk.Button(self, text = "Capture", command = self.takePic).grid(row = 3, column = 1, pady = 5, padx = 5)
## Turning on and off LED's
def LEDs(self):
GPIO.output(16, GPIO.HIGH)
time.sleep(2.5)
GPIO.output(16, GPIO.LOW)
#time.sleep(2.5)
## Setting up time stamp
def takePic(self):
now = datetime.datetime.now()
dateTimeStamp = now.strftime('%Y%m%d-%H%M%S')
global photoName
photoName = dateTimeStamp + ".jpg"
## Taking picture with datetime stamp
try:
camera.start_preview()
camera.annotate_background = picamera.Color('blue')
camera.annotate_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time.sleep(5)
camera.stop_preview()
self.fileName = photoName
camera.capture(self.fileName)
finally:
camera.close()
## Overlaying the watermark onto the original image
basePic = (self.fileName)
watermarkPic = 'Overlay.png'
base = Image.open(basePic)
watermark = Image.open(watermarkPic)
xyoffset = (0, 240)
base.paste(watermark, xyoffset, mask = watermark)
base.save(self.fileName)
## Mainloop
root = tk.Tk()
MainScreen(root)
root.after(3000, ButtonSwitch)
root.geometry('125x125+25+25')
root.mainloop()
root.destroy()