Skip to content

Instantly share code, notes, and snippets.

@akiraak
Last active July 13, 2018 00:23
Show Gist options
  • Save akiraak/e6a09dd58a5f2c1e117c321dd1253b75 to your computer and use it in GitHub Desktop.
Save akiraak/e6a09dd58a5f2c1e117c321dd1253b75 to your computer and use it in GitHub Desktop.
ラズパイでコンデジを作る ref: https://qiita.com/akiraak/items/59cde5b162b36fe360ce
[Desktop Entry]
Type=Application
Name=windowpy
Exec=/usr/bin/python2.7 /home/pi/projects/camera/camera.py
Terminal=false
import Tkinter as tk
import RPi.GPIO as GPIO
from datetime import datetime
import picamera
import os.path
import time
camera = picamera.PiCamera()
camera.resolution = (2592, 1944)
camera.rotation = 270
switchGPIO = 4
picturesPath = '/home/pi/projects/camera/output' # 写真の保存先
# フルスクリーンアプリを作るためのクラス
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master = master
pad = 3
self._geom = '200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth() - pad, master.winfo_screenheight() - pad))
master.bind('<Escape>', self.toggle_geom)
def toggle_geom(self, event):
geom = self.master.winfo_geometry()
print(geom, self._geom)
self.master.geometry(self._geom)
self._geom = geom
window = tk.Tk()
FullScreenApp(window)
window.title("Camera")
shotLabel = tk.Label(text='')
shotLabel.place(x=564, y=20)
shotLabel.config(font=('System', 32))
window.update()
# カメラのプレビュー画面を表示する
def previewCamera():
scale = 0.85
w = int(640 * scale)
h = int(480 * scale)
camera.start_preview(
fullscreen = False,
window = (0, 67, w, h))
def initGPIO():
GPIO.setmode(GPIO.BCM)
GPIO.setup(switchGPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def cleanGPIO():
GPIO.cleanup()
# 写真撮影
def takePicture():
filename = datetime.now().strftime('%Y%m%d-%H%M%S.jpg')
path = os.path.join(picturesPath, filename)
camera.capture(path)
checkSaved(path)
# 撮影できたかチェックしテキストを画面に表示する(まれに失敗する)
def checkSaved(path):
if os.path.exists(path) and os.path.getsize(path) > 0:
shotLabel['text'] = 'Saved'
else:
shotLabel['text'] = 'Error'
window.update()
time.sleep(3)
shotLabel['text'] = ''
window.update()
def main():
try:
initGPIO()
previewCamera()
switch = False
while True:
switchStatus = GPIO.input(switchGPIO) == GPIO.HIGH
if not switch:
if switchStatus:
takePicture()
switch = switchStatus
except KeyboardInterrupt:
pass
cleanGPIO()
camera.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment