LoginSignup
6
10

More than 5 years have passed since last update.

Raspberry PiでPythonからpicameraする

Last updated at Posted at 2019-03-06

モチベーション

Raspberry Pi 導入(カメラ付き)にて、コマンドラインからカメラ画像を保存することできたので、次はプログラムから実行する。

環境

RaspberryPi3B+
Raspbian 9.8
Python 2.7.13/Python 3.5.3
picamera 1.13

準備:Pythonの確認

RaspbianにはPythonは何がはいっ
ているんだろう?

i@raspberrypi:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22) 
[GCC 6.3.0 20170516] on linux2

2.7.13が入ってました。

pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux

3.5.3も入ってました。

/usr/binの下でpythonでgrepしたら

pi@raspberrypi:/usr/bin $ ls | grep python
〜略〜
python
python-config
python2
python2-config
python2.7
python2.7-config
python3
python3-config
python3.5
python3.5-config
python3.5m
python3.5m-config
python3m
python3m-config

一見すると2.7と3.5以外も入っているように見えるますが、どちらかにlinkされています。

pythonから画像取得

日付+時刻.jpgとして画像取得するコードは以下の通り。

picture.py
import time
import picamera
from datetime import datetime

with picamera.PiCamera() as camera:
 camera.resolution = (1024, 768)
 camera.start_preview()
 time.sleep(2)
 timestr = datetime.now().strftime('%Y%m%d%H%M%S')
 camera.capture(timestr+'.jpg')

python2,3どちらでも動きました。

$ python2 picture.py
$ python3 picture.py

プレビュー画面表示の camera.start_preview()
カメラをヲームアップさせるための time.sleep(2)
は削除しても画像取得できました。

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10