Skip to content

Instantly share code, notes, and snippets.

@jpoles1
Forked from kcranley1/workshop.py
Last active January 14, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoles1/5bc1b19f3fa1b553ffc351f3bd32b5d2 to your computer and use it in GitHub Desktop.
Save jpoles1/5bc1b19f3fa1b553ffc351f3bd32b5d2 to your computer and use it in GitHub Desktop.
A GUI program for use with the Raspberry Pi Camera
from Tkinter import *
import time
import picamera
import numpy as np
import io
from datetime import datetime, timedelta
camera = picamera.PiCamera()
ON = True
zoom = 1.0
previewTime = 3
awbcount = 0
effectno = 0
delay = 5
threshPercent = 1.8
step = 1
numImages = 1
captureCount = 0
numSav = 0
def arrange_windows(w, h):
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
h = h if h else hs
x = 0
y = 0
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
tk_margin_right = 5
camera.preview_window = (w + tk_margin_right, -38, ws - (w + tk_margin_right), hs)
def brightUp():
if camera.brightness < 100:
camera.brightness += 5
else:
camera.brightness = 100
w.set(camera.brightness)
# print("Brightness", camera.brightness)
preview()
def brightDown():
if camera.brightness < 10:
camera.brightness = 5
else:
camera.brightness -= 5
# print("Brightness", camera.brightness)
w.set(camera.brightness)
preview()
def rotate():
camera.rotation += 90
print("Rotation", camera.rotation, "degrees")
preview()
def hflip():
if camera.hflip == True:
camera.hflip = False
else:
camera.hflip = True
print("hFlip", camera.hflip)
preview()
def vflip():
if camera.vflip == True:
camera.vflip = False
else:
camera.vflip = True
print("vFlip", camera.vflip)
preview()
def preview():
camera.preview_fullscreen = False
camera.video_stabilization = True
camera.brightness = bright_slide.get()
print("Brightness", camera.brightness)
camera.contrast = contrast_slide.get()
print("Contrast", camera.contrast)
camera.saturation = sat_slide.get()
print("Saturation", camera.saturation)
camera.start_preview()
def still():
print("Press the Button!")
time.sleep(1)
camera.capture('/home/pi/Desktop/irimg.jpg')
def vid():
print("Press the Button!")
camera.start_recording('/home/pi/Desktop/irvid.h264')
time.sleep(15)
camera.stop_recording()
def sat():
camera.saturation=xy.get()
if camera.saturation < -99:
camera.saturation += 5
elif camera.saturation > 99:
camera.saturation -= 5
else:
camera.saturation +=5
xy.set(camera.saturation)
preview()
def sharp():
camera.sharpness += 10
print("Sharpness", camera.sharpness)
preview()
def zoomIn():
global zoom
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
if zoom >= 0.7:
zoom -= 0.05
x = (ws/2) - 0.5 * 400/(1 - zoom)
y = (hs/2) - 0.5 * 285/(1 - zoom)
camera.zoom = (x, y, zoom, zoom)
print("Zoom", zoom, "%")
else:
print("Max zoom!")
zoom = 0.7
print("Zoom", zoom, "%")
preview()
def zoomOut():
global zoom
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
if zoom <= 1.3:
zoom += 0.05
x = (ws/2) - 0.5 * 400/(1 - zoom)
y = (hs/2) - 0.5 * 285/(1 - zoom)
camera.zoom = (x, y, zoom, zoom)
print("Zoom", zoom, "%")
else:
print("Min zoom!")
zoom = 1.3
print("Zoom", zoom, "%")
preview()
def contrast():
camera.contrast = y.get()
if camera.contrast < -99:
camera.contrast += 5
elif camera.contrast > 99:
camera.contrast -= 5
else:
camera.contrast += 5
print("Contrast", camera.contrast)
y.set(camera.contrast)
preview()
def awb():
global awbcount
awbcount += 1
if awbcount == 1:
camera.AWB_MODES['auto']
print("AWB auto")
elif awbcount == 2:
camera.AWB_MODES['sunlight']
print("Sunlight")
elif awbcount == 3:
camera.AWB_MODES['cloudy']
print("Cloudy")
elif awbcount == 4:
camera.AWB_MODES['shade']
print("Shade")
elif awbcount == 5:
camera.AWB_MODES['tungsten']
print("Tungsten")
elif awbcount == 6:
camera.AWB_MODES['fluorescent']
print("Fluorescent")
elif awbcount == 7:
camera.AWB_MODES['incandescent']
print("Incandescent")
elif awbcount == 8:
camera.AWB_MODES['flash']
print("Flash")
elif awbcount == 9:
camera.AWB_MODES['horizon']
print("Horizon")
elif awbcount == 10:
camera.AWB_MODES['off']
print("Off")
else:
awbcount = 0
preview()
def effects():
global effectno
effectno += 1
if effectno == 1:
camera.image_effect = 'none'
elif effectno == 2:
camera.image_effect = 'negative'
elif effectno == 3:
camera.image_effect = 'solarize'
elif effectno == 4:
camera.image_effect = 'sketch'
elif effectno == 5:
camera.image_effect = 'denoise'
elif effectno == 6:
camera.image_effect = 'emboss'
elif effectno == 7:
camera.image_effect = 'oilpaint'
elif effectno == 8:
camera.image_effect = 'hatch'
elif effectno == 9:
camera.image_effect = 'gpen'
elif effectno == 10:
camera.image_effect = 'pastel'
elif effectno == 11:
camera.image_effect = 'watercolor'
elif effectno == 12:
camera.image_effect = 'film'
elif effectno == 13:
camera.image_effect = 'blur'
elif effectno == 14:
camera.image_effect = 'saturation'
elif effectno == 15:
camera.image_effect = 'colorswap'
elif effectno == 16:
camera.image_effect = 'washedout'
elif effectno == 17:
camera.image_effect = 'posterise'
elif effectno == 18:
camera.image_effect = 'colorpoint'
elif effectno == 19:
camera.image_effect = 'colorbalance'
elif effectno == 20:
camera.image_effect = 'cartoon'
elif effectno == 21:
camera.image_effect = 'deinterlace1'
elif effectno == 22:
camera.image_effect = 'deinterlace2'
print("image effect",camera.image_effect)
preview()
def demo():
for x in range(1, 10):
awb()
preview()
time.sleep(3)
def filenames():
frame = 0
fileName = time.strftime("/home/pi/Desktop/images/%d-%m-%Y:%H:%M:%S",time.localtime())
while frame < 1:
yield '%s%00d.jpg' % (fileName, frame)
frame += 1
root = Tk()
root.title("Pi Camera")
arrange_windows(180, None)
btn_height = 2
btn_width = 20
btn_pady = 5
slider_height = 25
slider_width = 180
quitButton = Button(root, bg="red", text=" Quit ", command=exit)
quitButton.config(width=btn_width, height=btn_height)
quitButton.grid(row=0, pady=btn_pady)
takeStillButton = Button(root, text=" Take still ", command=still)
takeStillButton.config(width=btn_width, height=btn_height)
takeStillButton.grid(row=1, pady=btn_pady)
takeVideoButton = Button(root, text=" Take video ", command=vid)
takeVideoButton.config(width=btn_width, height=btn_height)
takeVideoButton.grid(row=2, pady=btn_pady)
bright_slide = Scale(root, from_=25, to=75, resolution=5, orient=HORIZONTAL)
bright_slide.config(width=slider_height, length=slider_width)
bright_slide.grid(row=3)
bright_slide.set(camera.brightness)
bright_slide.bind("<ButtonRelease-1>", lambda x: preview())
bright_label = Label(root, text="Brightness")
bright_label.grid(row=4)
contrast_slide = Scale(root, from_=-50, to=50, resolution=1, orient=HORIZONTAL)
contrast_slide.config(width=slider_height, length=slider_width)
contrast_slide.grid(row=5)
contrast_slide.set(camera.contrast)
contrast_slide.bind("<ButtonRelease-1>", lambda x: preview())
contrast_label = Label(root, text="Contrast")
contrast_label.grid(row=6)
sat_slide = Scale(root, from_=-100, to=100, resolution=1, orient=HORIZONTAL)
sat_slide.config(width=slider_height, length=slider_width)
sat_slide.grid(row=7)
sat_slide.set(camera.saturation)
sat_slide.bind("<ButtonRelease-1>", lambda x: preview())
sat_label = Label(root, text="Saturation")
sat_label.grid(row=8)
#zoomInButton = Button(root, text = "Zoom in", command = zoomIn)
#zoomInButton.grid(row = 24, column = 0)
#zoomOutButton = Button(root, text = "Zoom out", command = zoomOut)
#zoomOutButton.grid(row = 24, column = 1)
def callback(_):
preview()
root.unbind('<Visibility>') # only call `callback` the first time `root` becomes visible
root.bind('<Visibility>', callback)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment