2.1 打开python3,建立名为camera.py的新文件,注意,千万不能命名为picamera.py
2.2 在文件中写入一下代码,运行即可,注意:远程连接(例如:SSH和VNC)时不允许访问摄像头
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()
2.3 如果图像时上下颠倒的,可以用一下代码实现翻转
camera.rotation = 180
camera.start_preview()
sleep(10)
camera.stop_preview()
可以设置旋转的度数为90,180或270,也可以设置为0
2.4 通过改变alpha的值,可以修改摄像头拍摄图像的透明度
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview(alpha=200)
sleep(10)
camera.stop_preview()
alpha的取值范围为0-255
3.1 修改代码:减少sleep并添加camera.capture()一行
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
捕获图片前,至少要给传感器两秒钟时间感光
3.2 增加一个循环就能实现连拍,每隔5秒拍一张,拍完后预览关闭,桌面上就有五张图了
camera.start_preview()
for i in range(5):
sleep(5)
camera.capture('/home/pi/Desktop/image%s.jpg' % i)
camera.stop_preview()
4.1 修改代码:用start_recording()和stop_recording()代替capture()
camera.start_preview()
camera.start_recording('/home/pi/video.h264')
sleep(10)
camera.stop_recording()
camera.stop_preview()
运行程序,将拍摄10秒的视屏,然后关闭预览
4.2. 播放视屏需要执行如下指令:
omxplayer video.h264
4.3 播放时可能会出现播放速度比实际速度快的情况,这是omxplayer的快速帧速率导致的
摄像头的分辨率可以自己设定,但要记住,照片的最大分辨率时2592x1944,视频的最大分辨率为1920x1080,可以通过一下代码设定分辨率,为了配合最大分辨率,需要设置帧速率为15
camera.resolution = (2592, 1944)
camera.framerate = 15
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/max.jpg')
camera.stop_preview()
最小分辨率允许设为64x64
用annotate_text很方便地为图像添加文字
camera.start_preview()
camera.annotate_text = "Hello world!"
sleep(5)
camera.capture('/home/pi/Desktop/text.jpg')
camera.stop_preview()
改变亮度设置,范围是0-100,预设为50
camera.start_preview()
camera.brightness = 70
sleep(5)
camera.capture('/home/pi/Desktop/bright.jpg')
camera.stop_preview()
在循环中调整亮度,并标注亮度等级
camera.start_preview()
for i in range(100):
camera.annotate_text = "Brightness: %s" % i
camera.brightness = i
sleep(0.1)
camera.stop_preview()
调节文字大小,6-160,预设为32
camera.annotate_text_size = 50
改变文字的颜色,首先要引入Color,然后就能用如下代码实现
from picamera import PiCamera, Color
camera.start_preview()
camera.annotate_background = Color('blue')
camera.annotate_foreground = Color('yellow')
camera.annotate_text = " Hello world "
sleep(5)
camera.stop_preview()
camera.image_effect为图片添加特殊效果,可选择的参数有:none,negative,solarize,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolor,film,blur,saturation,colorswap,washedout,posterise,colorpoint,colorbalance,cartoon,deinterlace1和deinterlace2。预设为none。
camera.start_preview()
camera.image_effect = 'colorswap'
sleep(5)
camera.capture('/home/pi/Desktop/colorswap.jpg')
camera.stop_preview()
用循环显示不同效果的预览
camera.start_preview()
for effect in camera.IMAGE_EFFECTS:
camera.image_effect = effect
camera.annotate_text = "Effect: %s" % effect
sleep(5)
camera.stop_preview()
camera.awb_mode可以设置白平衡,可选参数有:off, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandescent, flash和horizon,预设为auto
camera.start_preview()
camera.awb_mode = 'sunlight'
sleep(5)
camera.capture('/home/pi/Desktop/sunlight.jpg')
camera.stop_preview()
可以用camera.AWB_MODES循环展示多个白平衡模式
camera.exposure_mode可以设置曝光,可选参数有:off,auto,night,nightpreview,nightpreview,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks。预设是auto
camera.start_preview()
camera.exposure_mode = 'beach'
sleep(5)
camera.capture('/home/pi/Desktop/beach.jpg')
camera.stop_preview()
可以用camera.EXPOSURE_MODES循环展示多个曝光模式