本篇先从最基本的应用开始讲起,再详细介绍一些更高阶的设定。

内容说明
操作系统Raspberry Pi OS
IDEThonny Python IDE

1. 基本实现

from picamera import PiCamera
from time import sleep
with PiCamera() as camera:
	camera.start_preview()  # 树莓派上显示视频
	camera.start_recording('/home/pi/Desktop/video.h264')  # 存储视频到相应的文件夹下
	sleep(5)
	camera.stop_recording()  # 停止存储
	camera.stop_preview()  # 停止显示

函数start_recording()支持的视频格式如下:
‘h264’ - Write an H.264 video stream
‘mjpeg’ - Write an M-JPEG video stream
‘yuv’ - Write the raw video data to a file in YUV420 format
‘rgb’ - Write the raw video data to a file in 24-bit RGB format
‘rgba’ - Write the raw video data to a file in 32-bit RGBA format
‘bgr’ - Write the raw video data to a file in 24-bit BGR format
‘bgra’ - Write the raw video data to a file in 32-bit BGRA format

2. 高阶设定

基本设置

设置内容代码说明
分辨率camera.resolution = (1920, 1080)静态照片的最大分辨率为2592×1944,视频录制的最大分辨率为1920×1080。最低分辨率为64×64。
每秒帧数camera.framerate = 30

画面设置

设置内容代码说明
旋转camera.rotation = 0
水平翻转camera.vflip = False
垂直翻转camera.hflip = False
添加文字camera.annotate_text = "Hello world!"
更改添加文本的外观camera.annotate_text_size = 50
图像稳定camera.video_stabilization = False当设置为 True 时,这会打开视频稳定功能。 仅当摄像头模块在录制时移动时才需要此功能,例如,如果它连接到机器人或被携带,以减少捕获视频的抖动。
裁剪图片camera.crop = (0.0, 0.0, 1.0, 1.0)

画面效果

设置内容代码说明
饱和度camera.saturation = 0
亮度camera.brightness = 50取值范围: 0 and 100;默认值是 50
阴影camera.sharpness = 0
对比度camera.contrast = 0这将设置图像的对比度。较高的数字将使事情看起来更加戏剧化和鲜明; 较低的数字将使事情看起来更加褪色。您可以使用100之间的任何数字来获得最小对比度,并使用100来获得最大对比度。
色彩效果camera.color_effects = NoneThis changes the colour effect currently in use by the camera. Normally, this setting should be left alone, but if you provide a pair of numbers you can alter the way the camera records colour: try (128, 128) to create a black and white image.
调节模式camera.awb_mode = 'auto'This sets the automatic white balance mode of the camera, and can be set to any one ofthe following modes: off, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandescent, flash, or horizon. If you find your pictures and videos look a little blue or yellow, try a different mode.
补光camera.exposure_compensation = 0This sets the exposure compensation of the camera, allowing you to manually control how much light is captured for each image. Unlike changing the brightness setting, this actually controls the camera itself. Valid values range from -25 for a very dark image to 25 for a very bright image.
曝光camera.exposure_mode = 'auto'This sets the exposure mode, or the logic the Camera Module uses to decide how an image should be exposed. Possible modes are: off, auto, night, backlight, spotlight, sports, snow, beach, verylong, fixedfps, antishake, and fireworks.
曝光光亮camera.meter_mode = 'average'This controls how the camera decides on the amount of available light when setting its exposure. The default averages the amount of light available throughout the whole picture; other possible modes are backlit, matrix, and spot.
图像效果camera.image_effect = 'none'图片效果:可选 blur, cartoon, colorbalance, colorpoint, colorswap, deinterlace1, deinterlace2, denoise, emboss, film, gpen, hatch, negative, none, oilpaint, pastel, posterise, saturation, sketch, solarize, washedout, and watercolor.
IOScamera.ISO = 0This changes the ISO setting of the camera, which affects how sensitive it is to light. By default, the camera adjusts this automatically depending on the available light. You can set the ISO yourself using one of the following values: 100, 200, 320, 400, 500, 640, 800. The higher the ISO, the better the camera will perform in low-light environments but the grainier the image or video it captures.

参考:
树莓派摄像头Camera的使用
9. API - The PiCamera Class

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐