Raspberry Pi Camera Setting
Table of Contents
Rasberry pi camera
There are two ways to convert images or record Raspberry Pi Camera videos using Python scripts
You call the raspistill command with subprocess.call.
- Or you can use the picamera module, with which you can directly control the camera using python code.
This section only covers the second variant. The first thing you need to do is get the package Install python3-picamera for Python 3 or python-picamera for Python 2 – or both versions:
1 |
sudo apt - get install python - picamera python3 - picamera |
In the Python code, you first create a PiCamera object, which then gives you a lot of Methods. The most important are:
- capture: takes a photo and saves it to a file.
- start_recording: starts a video recording and saves the film in a File.
- stop_recording: ends the video recording.
- close: closes access to the camera.
The following paragraphs describe only the most important functions of the picamera Module. A comprehensive reference of all classes and methods as well as a lot of examples can be found in the online documentation. There are a lot of them there advanced use cases described, e.g. the transmission of images and Videos on the network or the use of a buffer for a predefined time, whereby the oldest recording is then overwritten by the current one becomes (circular stream).
Amazon Purchase Links:
Wireless Keyboard and Mouse for raspberry pi:
Night vision Camera for Raspberry Pi:
Oled HDMI touch display for raspberry pi:
Other Tools and Components:
Super Starter kit for Beginners
PCB small portable drill machines
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Take pictures in its simplest form, the application of the picamera module looks like this:
1 2 3 4 5 6 7 8 9 |
#! / usr / bin / python3 import picamera camera = picamera. PiCamera () camera. capture ('image. jpg') camera. close () |
capture supports the bitmap formats JPEG, PNG and GIF, among others. The method tries to identify the desired format based on the file name. At File names without an identifier must be specified using the optional parameter format = ‘xxx’. For the JPEG format, you can use the optional parameter
quality = n specify the desired image quality in the range between 1 and 100.
The default setting is 85. The higher the value, the better the image quality. At the same time, however, the size of the file also increases. The standard image size is 1920 x 1200 pixels. Do you want bigger or bigger ones smaller pictures, specify resize = (width, height). However, you have to be careful that the ratio of width and height remains unchanged – otherwise the images are distorted!
If you want the recording to be very fast, specify use_video_port = True.
The picamera module then picks up the video interface of the camera. That can be done clearly faster, but the resulting images are less sharp.
1 2 3 |
camera. capture ('image. jpg', resize = (480, 300), use_video_port = True, quality = 80) |
With capture_sequence you can record entire sequences of images. In the first parameter must a list of files to be passed. With the following parameters takes picamera an impressive 16 frames per second on:
1 2 3 4 5 |
filenames = ['image% 02 d.jpg'% i for i in range (100)] camera. capture_sequence (filenames, resize = (480, 300), use_video_port = True, quality = 80) |
capture_continuous works similarly to capture_sequence, but it is generated
The method the file names themselves. To do this, build the character string in the file name {counter} or {timestamp}. This pattern is then followed by a scrolling counter or replaced by the current date and time. Optional you can format these numbers or times with format codes for example:
1 2 3 |
'image {counter: 03 d}. jpg 'three-digit counter 'image {timestamp:% H -% M -% S}. jpg 'Time information in the form 23 -59 -59 |
You can even combine {counter} and {timestamp} in one string. capture_sequence returns a generator, so it must be used in a loop. In each loop pass, the method creates a generator object, which you do not have to evaluate further.
The following script runs endlessly until it is terminated with (Ctrl) + (C). It takes images continuously and saves them under the name image-nn.jpg, where nn is the Second of the recording time. Existing images will be overwritten.
The current directory therefore always contains image files with recordings of the last 60 seconds available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#! / usr / bin / python3 import picamera, time camera = picamera. PiCamera () filenames = 'image - {timestamp:% S}. jpg ' try: for obj in camera. capture_continuous (filenames, resize = (480, 300)): time. sleep (0.1) except KeyboardInterrupt: pass # do not show any error messages finally: camera. close () |
Record videos
start_recording records videos and encodes them either in H.264 or in M-JPEG format. The desired format either has the extension .h264 or .mjpeg of the file name or must be entered with the optional parameter format = ‘h264 | mjpeg’. It is mostly before the start of the recording It is advisable to reduce the resolution with camera.resulution.
stop_recording ends the recording. When you record a video of 10 seconds , it is recommended to use wait_ instead of time.sleep bridge recording. The advantage of this method over sleep is that an error occurred during recording (e.g. insufficient space to store the or save the video file) leads to the immediate end of the program. The error message occurs with sleep on the other hand only after the specified sleep break has elapsed. The following applet takes a 10 second video at a resolution of 800 x 600 pixels on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#! / usr / bin / python3 import picamera camera = picamera. PiCamera () try: camera. resolution = (800, 600) camera. start_recording ('video. h264') camera. wait_recording (10) camera. stop_recording () except KeyboardInterrupt: passport finally: camera. close () |
note:
Play videos: On the Raspberry Pi you can play H.264 videos directly with the omxplayer. However, this program is overwhelmed with M-JPEG recordings. The remedy Installation of the video player VLC with sudo apt-get install vlc. The program is, however, quite extensive and takes up around 100 MB of storage space. In addition, the VLC version from the Raspbian package sources does not use any hardware Accelerated playback. That’s why the program is only for videos in lower resolution suitable.
Control camera parameters
There are many camera parameters you can set before taking a photo or video set, e.g. the resolution, the brightness or the contrast (see table1). A detailed reference of all parameters can be found in the API documentation of the picamera module:
Table 1: Selected camera parameters
parameter | importance | values | Default |
Brightness | brightness | 0 to 100 | 50 |
Contrast | contrast | –100 to 100 | 0 |
Crop | To the crop the image | (x, y, b, h) | (0.0, 0.0, 1.0, 1.0) |
Framerate | frames per second | 1 to 30 | 30 |
Resolution | Image resolution | (b, h) | (1920, 1200) |
Rotation | Rotation of the image | 0, 90, 180, 270 | 0 |
Saturation | Color saturation | –100 to 100 | 0 |
sharpness | Sharpness | –100 to 100 | 0 |
The following mini-program creates nine recordings with different settings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#! / usr / bin / python3 import picamera camera = picamera. PiCamera () camera. resolution = (1280, 960) cnt = 1 for c in (-10, 0, 10): for b in (40, 50, 60): camera. brightness = b camera. contrast = c camera. capture ('image -% d. jpg'% cnt) cnt + = 1 camera. close () |