Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Camera menu and effect for Nakayoshi Camera
#!/usr/bin/python
# -*0 coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import picamera
import os
import pygame
import syslog
# GPIO pin
LeftSh = 17
RightSh = 18
UpSw = 27
RightSw = 22
LowSw = 23
LeftSw = 24
RightLowSw = 10
LeftLowSw = 9
# GPIO setting
GPIO.setmode(GPIO.BCM)
GPIO.setup(RightSh, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LeftSh, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(UpSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(RightSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LeftSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(RightLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LeftLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Camera Setting
shutter_numb = 0
preview_numb = 0
# global
SleepStepSec = 0.1
home_dir = '/home/pi/camera/data'
camera_mode = 0
effect_type = 0
# mode
auto = 0
effect = 1
video = 2
timelapse = 3
reset = 4
MAX_CAMERA_MODE = 4
# effect
sketch = 0
oilpaint = 1
pastel = 2
watercolour = 3
film = 4
saturation = 5
posterise = 6
cartoon = 7
MAX_EFFECT_TYPE = 7
# position
pygame.init()
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
print "Framebuffer size: %d x %d" % (size[0], size[1])
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
keifont_title = pygame.font.Font(os.path.join(home_dir, 'keifont.ttf'), 80)
keifont_menu = pygame.font.Font(os.path.join(home_dir, 'keifont.ttf'), 40)
def screen_clear():
screen.fill((0,0,0))
pygame.display.update()
def screen_opening():
title = keifont_title.render(u"なかよしデジカメ", True, (180,0,0))
screen.fill((0,230,0))
screen.blit(title, (20,150))
pygame.display.update()
time.sleep(3)
screen_clear()
def screen_menu():
menu_1 = keifont_menu.render(u"おーと", True, (180,0,0))
menu_2 = keifont_menu.render(u"えふぇくと", True, (180,0,0))
menu_3 = keifont_menu.render(u"むーびー", True, (180,0,0))
menu_4 = keifont_menu.render(u"たいむらぷす", True, (180,0,0))
menu_5 = keifont_menu.render(u"りせっと", True, (180,0,0))
screen.fill((0,230,0))
screen.blit(menu_1, (20,20))
screen.blit(menu_2, (20,70))
screen.blit(menu_3, (20,120))
screen.blit(menu_4, (20,170))
screen.blit(menu_5, (20,220))
if camera_mode == auto:
pygame.draw.circle(screen, (255,0,0), (10,40), 10)
elif camera_mode == effect:
pygame.draw.circle(screen, (255,0,0), (10,90), 10)
effect_1 = keifont_menu.render(u"すけっち", True, (180,0,0))
effect_2 = keifont_menu.render(u"あぶらえ", True, (180,0,0))
effect_3 = keifont_menu.render(u"ぱすてる", True, (180,0,0))
effect_4 = keifont_menu.render(u"すいさい", True, (180,0,0))
effect_5 = keifont_menu.render(u"ふぃるむ", True, (180,0,0))
effect_6 = keifont_menu.render(u"あざやか", True, (180,0,0))
effect_7 = keifont_menu.render(u"ぽすたー", True, (180,0,0))
effect_8 = keifont_menu.render(u"まんが", True, (180,0,0))
screen.blit(effect_1, (350,20))
screen.blit(effect_2, (350,70))
screen.blit(effect_3, (350,120))
screen.blit(effect_4, (350,170))
screen.blit(effect_5, (350,220))
screen.blit(effect_6, (350,270))
screen.blit(effect_7, (350,320))
screen.blit(effect_8, (350,370))
print "effect_type"
print effect_type
if effect_type == sketch:
pygame.draw.circle(screen, (255,0,0), (330,40), 10)
elif effect_type == oilpaint:
pygame.draw.circle(screen, (255,0,0), (330,90), 10)
elif effect_type == pastel:
pygame.draw.circle(screen, (255,0,0), (330,140), 10)
elif effect_type == watercolour:
pygame.draw.circle(screen, (255,0,0), (330,190), 10)
elif effect_type == film:
pygame.draw.circle(screen, (255,0,0), (330,240), 10)
elif effect_type == saturation:
pygame.draw.circle(screen, (255,0,0), (330,290), 10)
elif effect_type == posterise:
pygame.draw.circle(screen, (255,0,0), (330,340), 10)
elif effect_type == cartoon:
pygame.draw.circle(screen, (255,0,0), (330,390), 10)
else:
print "error"
elif camera_mode == video:
pygame.draw.circle(screen, (255,0,0), (10,140), 10)
elif camera_mode == timelapse:
pygame.draw.circle(screen, (255,0,0), (10,190), 10)
elif camera_mode == reset:
pygame.draw.circle(screen, (255,0,0), (10,240), 10)
else:
print "error"
pygame.display.update()
def load():
global shutter_numb
filename = os.path.join(home_dir, 'camera.set')
fp = open(filename)
tmp_shutter_numb = fp.readlines()
tmp2_shutter_numb = tmp_shutter_numb[0].rstrip()
shutter_numb = int(tmp2_shutter_numb)
fp.close()
print "shutter_numb:" + str(shutter_numb)
def save():
filename = os.path.join(home_dir, 'camera.set')
fp = open(filename, 'w')
fp.write(str(shutter_numb))
fp.close()
def shutter(channel):
global shutter_numb
global preview_numb
print "shutter"
screen_clear()
shutter_numb += 1
preview_numb = shutter_numb
save()
filename = os.path.join(home_dir, str(shutter_numb) + '.jpg')
photofile = open(filename, 'wb')
if camera_mode == auto:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
elif camera_mode == effect:
if effect_type == sketch:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'sketch'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == oilpaint:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'oilpaint'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == pastel:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'pastel'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == watercolour:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'watercolor'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == film:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'film'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == saturation:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'saturation'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == posterise:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'posterise'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
if effect_type == cartoon:
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.image_effect = 'cartoon'
camera.start_preview()
time.sleep(2) # initialize camera
camera.capture(photofile)
photofile.close()
else:
print "error"
screen_clear()
screen_menu()
def KeepWatchForSeconds(seconds):
GoFlag = True
if seconds < 1:
seconds = 1
while seconds > 0:
time.sleep(SleepStepSec)
seconds -= SleepStepSec
if (GPIO.input(LeftSh) == True):
GoFlag = False
break
return GoFlag
def CallShutdown():
syslog.syslog(syslog.LOG_NOTICE, "Going shutdown by GPIO.")
os.system("/sbin/shutdown -h now 'Poweroff by GPIO'")
def preview(channel):
if KeepWatchForSeconds(5):
CallShutdown()
global preview_numb
print "preview"
filename = os.path.join(home_dir, str(preview_numb) + '.jpg')
img = pygame.image.load(filename).convert()
img = pygame.transform.scale(img, (pygame.display.Info().current_w, pygame.display.Info().current_h))
screen.blit(img, (0,0))
pygame.display.update()
preview_numb -= 1
if preview_numb < 1:
preview_numb = shutter_numb
def mode_up(channel):
global camera_mode
camera_mode += 1
if camera_mode > MAX_CAMERA_MODE:
camera_mode = 0
screen_clear()
screen_menu()
def mode_down(channel):
global camera_mode
camera_mode -= 1
if camera_mode < 0:
camera_mode = MAX_CAMERA_MODE
screen_clear()
screen_menu()
def opt_up(channel):
global effect_type
if camera_mode == effect:
effect_type += 1
if effect_type > MAX_EFFECT_TYPE:
effect_type = 0
screen_clear()
screen_menu()
def opt_down(channel):
global effect_type
if camera_mode == effect:
effect_type -= 1
if effect_type < 0:
effect_type = MAX_EFFECT_TYPE
screen_clear()
screen_menu()
GPIO.add_event_detect(RightSh, GPIO.FALLING, callback=shutter, bouncetime=300)
GPIO.add_event_detect(LeftSh, GPIO.FALLING, callback=preview, bouncetime=300)
GPIO.add_event_detect(UpSw, GPIO.FALLING, callback=mode_down, bouncetime=300)
GPIO.add_event_detect(LowSw, GPIO.FALLING, callback=mode_up, bouncetime=300)
GPIO.add_event_detect(RightSw, GPIO.FALLING, callback=opt_up, bouncetime=300)
GPIO.add_event_detect(LeftSw, GPIO.FALLING, callback=opt_down, bouncetime=300)
#GPIO.setup(RightLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#GPIO.setup(LeftLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP)
screen_opening()
load()
try:
screen_clear()
screen_menu()
GPIO.wait_for_edge(LeftLowSw, GPIO.FALLING)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.