Skip to content
This repository has been archived by the owner on Aug 7, 2022. It is now read-only.

Commit

Permalink
Initial release of first basic recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
baqwas committed Jul 26, 2018
1 parent be10a13 commit 1b5897c
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 0 deletions.
76 changes: 76 additions & 0 deletions basic/CaptureResizedImage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CaptureResizedImage.py
# capture image in primary resolution and save to file at secondary resolution
#
# Copyright 2018 Matha Goram <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Reference: https://picamera.readthedocs.io/en/release-1.13/api_camera.html
#
# Acknowledgement: ALL code adapteed from PiCamera documentation referenced above

import getopt # parse command line options
from picamera import PiCamera # RPi camera module
import sys
from time import sleep

def CaptureResizedImage(argv):
bound = lambda n, minn, maxn: max(min(maxn, n), minn) # utility function to clamp integer value range

# command line parameter processing
delay = 2 # warm-up time delay in seconds
frate = 30 # framerate, frames per second
ofile = "cap2resize.jpg" # default output filename
h1 = 1024 # horizontal resolution in pixels for camera
v1 = 768 # veritical resolution in pixels for camera
h2 = 1024 # horizontal resolution in pixels for image
v2 = 768 # veritical resolution in pixels for image
usage = "usage: CaptureResizedImage.py -o <output filename> -h <pixel> -w <pixel> -x <pixel> -y <pixel>"
try:
# argv: argument list to be parsed
# option letters with colon suffix for mandatory parameter value (viz. i and o)
# long_options with equal suffix for expected parameter value
opts, args = getopt.getopt(argv, "d:f:h:?o:v:x:y:", ["delay=", "frate=", "help", "horz=" "ofile=", "vert=", "ximage=", "yimage="])
for opt, arg in opts:
if opt in ("-d", "--delay"):
delay = int(arg) # use intrinsic function
elif opt in ("-f", "--frate"):
frate = bound(int(arg), 1, 120) # framerate in frames per second; extreme limits
elif opt in ("-h", "--horz"):
h1 = bound(int(arg), 1, 4096) # horizonal resolution in pixels; needs bounds check, please
elif opt == ("-?", "--help"):# help option keywords
print(usage)
sys.exit() # exit without abend
elif opt in ("-o", "--ofile"):
ofile = arg
elif opt in ("-v", "--vert"):
v1 = bound(int(arg), 1, 4096) # vertical resolution in pixels; need bounds check, please
except getopt.GetoptError: # unrecognized option in the argument list
print(usage) # standard command line syntax
sys.exit(1) # invalid parameters specified when invoking program execution
# camera work processing
myCamera = PiCamera(resolution=(h1, v1), framerate=fmt) # instantiate access to camera module overriding some parameters
myCamera.resolution = (h1, v1) # set (width, height) in pixels
myCamera.start_preview() # display preview overlay
sleep(delay) # Camera warm-up time
myCamera.capture(ofile) # capture image, store in specified filename per filetype format
return 0

if __name__ == '__main__':
sys.exit(CaptureResizedImage(sys.argv[1:]))
115 changes: 115 additions & 0 deletions basic/CaptureToFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CaptureToFile.py
# capture image to file
#
# Copyright 2018 Matha Goram <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Reference
# https://picamera.readthedocs.io/en/release-1.13/api_camera.html
# Basic Recipes 3.1 Capturing to a file
#
# Acknowledgement: ALL code adapteed from PiCamera documentation referenced above

import getopt # parse command line options
from picamera import PiCamera # RPi camera module
import sys
from time import sleep

def CaptureToFile(argv):
bound = lambda n, minn, maxn: max(min(maxn, n), minn) # function to check on min/max bounds

# command line parameter processing
delay = 2 # warm-up time delay in seconds
ofile = "../images/CaptureToFile.jpg" # default output filename
horz = 1024 # horizontal resolution in pixels
vert = 768 # veritical resolution in pixels
usage = "usage: CaptureToFile.py -o <output filename>"
try:
# argv: argument list to be parsed
# option letters with colon suffix for mandatory parameter value (viz. i and o)
# long_options with equal suffix for expected parameter value
opts, args = getopt.getopt(argv, "d:h:?o:v:", ["delay=", "help", "horz=" "ofile=", "vert="])
for opt, arg in opts:
if opt in ("-d", "--delay"):
delay = int(arg) # use intrinsic function
elif opt in ("-h", "--horz"):
horz = bound(int(arg), 1, 4096) # horizonal resolution in pixels; needs bounds check, please
elif opt == ("-?", "--help"):# help option keywords
print(usage)
sys.exit() # exit without abend
elif opt in ("-o", "--ofile"):
ofile = arg
elif opt in ("-v", "--vert"):
vert = bound(int(arg), 1, 4096) # vertical resolution in pixels; need bounds check, please
except getopt.GetoptError: # unrecognized option in the argument list
print(usage) # standard command line syntax
sys.exit(1) # invalid parameters specified when invoking program execution
# camera work processing
"""
class:
PiCamera
parameters:
camera_num=0
stereo_mode='none'
stereo_decimate=False
resolution=None
framerate=None
sensor_mode=0
led_pin=None
clock_mode='reset'
framerate_range=None
"""
completionCode = 0 # temporary use to avoid error log write operations
try:
myCamera = PiCamera() # instantiate interface to camera module
myCamera.resolution = (horz, vert) # set (width, height) in pixels
myCamera.start_preview() # display preview overlay
sleep(delay) # Camera warm-up time
"""
method:
capture
parameters:
output
format
use_video_port
resize
splitter_port
bayer
**options
"""
myCamera.capture(ofile) # capture image, store in specified filename per filetype format
# image not flipped since this is a generic exercise
except picamera.PiCameraError:
completionCode = -2 # further granularity avoided in this sample code
except:
completionCode = -1 # error external to PiCamera usage
else:
completionCode = 1 # sailed through
finally:
"""
method:
close
parameters:
none
"""
myCamera.close() # good housekeeping
return completionCode
if __name__ == '__main__':
sys.exit(CaptureToFile(sys.argv[1:]))
74 changes: 74 additions & 0 deletions basic/CaptureToPIL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CaptureToStream.py
# capture image to stream and read into PIL image object
#
# Copyright 2018 Matha Goram <[email protected]>
#
# v0.1
# 2018-07-08
# armw
# initial version
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Reference: https://picamera.readthedocs.io/en/release-1.13/api_camera.html
#
# Acknowledgement: ALL code adapteed from PiCamera documentation referenced above

import getopt # parse command line options
from PIL import Image # Python Image Library
from io import BytesIO # stream input/output operations
from picamera import PiCamera # RPi camera module
import sys
from time import sleep

def CaptureToPIL(argv):
bound = lambda n, minn, maxn: max(min(maxn, n), minn)

# command line parameter processing
delay = 2 # warm-up time delay in seconds
fmt = "jpeg" # default capture file format
usage = "usage: CaptureToPIL.py -d <delay> -f <file format>"
try:
# argv: argument list to be parsed
# option letters with colon suffix for mandatory parameter value (viz. i and o)
# long_options with equal suffix for expected parameter value
opts, args = getopt.getopt(argv, "d:f:h", ["delay=", "format=", "help"])
for opt, arg in opts:
if opt in ("-d", "--delay"):
delay = int(arg) # use intrinsic function
elif opt in ("-f", "--format"):
fmt = arg # horizonal resolution in pixels; needs bounds check, please
elif opt == ("-h", "--help"):# help option keywords
print(usage)
sys.exit() # exit without abend
except getopt.GetoptError: # unrecognized option in the argument list
print(usage) # standard command line syntax
sys.exit(1) # invalid parameters specified when invoking program execution
# camera work processing
myStream = BytesIO() # instantiate in memory stream
myCamera = PiCamera() # instantiate access to camera module
myCamera.start_preview() # display preview overlay
sleep(delay) # Camera warm-up time
myCamera.capture(myStream, foramat=fmt)
myStream.seek(0) # read content from beginning
myImage = Image.open(myStream) # open and identify the given stream using lazy operation
# will need to use load method to force loading
return 0

if __name__ == '__main__':
sys.exit(CaptureToPIL(sys.argv[1:]))
69 changes: 69 additions & 0 deletions basic/CaptureToStream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CaptureToStream.py
#
# Copyright 2018 Matha Goram <[email protected]>
#
# v0.1
# 2018-07-08
# armw
# initial version
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Reference: https://picamera.readthedocs.io/en/release-1.13/api_camera.html
#
# Acknowledgement: ALL code adapteed from PiCamera documentation referenced above

import getopt # parse command line options
from io import BytesIO # stream input/output operations
from picamera import PiCamera # RPi camera module
import sys
from time import sleep

def CaptureToStream(argv):
bound = lambda n, minn, maxn: max(min(maxn, n), minn)

# command line parameter processing
delay = 2 # warm-up time delay in seconds
format = "jpeg" # default capture file format
usage = "usage: CaptureToStream.py -d <delay> -f <file format>"
try:
# argv: argument list to be parsed
# option letters with colon suffix for mandatory parameter value (viz. i and o)
# long_options with equal suffix for expected parameter value
opts, args = getopt.getopt(argv, "d:f:h", ["delay=", "format=", "help"])
for opt, arg in opts:
if opt in ("-d", "--delay"):
delay = int(arg) # use intrinsic function
elif opt in ("-f", "--format"):
format = arg # horizonal resolution in pixels; needs bounds check, please
elif opt == ("-h", "--help"):# help option keywords
print(usage)
sys.exit() # exit without abend
except getopt.GetoptError: # unrecognized option in the argument list
print(usage) # standard command line syntax
sys.exit(1) # invalid parameters specified when invoking program execution
# camera work processing
myStream = BytesIO()
myCamera = PiCamera() # instantiate access to camera module
myCamera.start_preview() # display preview overlay
sleep(delay) # Camera warm-up time
myCamera.capture(myStream, format) # capture stream using specified format
return 0

if __name__ == '__main__':
sys.exit(CaptureToStream(sys.argv[1:]))
31 changes: 31 additions & 0 deletions basic/CaptureToStream0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CaptureToStream.py
#
# Copyright 2018 <pi@raspbari1>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#

import sys

def main(args):
return 0

if __name__ == '__main__':
sys.exit(main(sys.argv))
Loading

0 comments on commit 1b5897c

Please sign in to comment.