本文不知道为什么就乱掉了,又用markdown的格式写了一个,看这里:

一.摄像头安装

如图从两端拔起,插入,再按紧

树莓派拍照python 树莓派摄像头拍照python_代码实现

 

二.摄像头预览

1. 打开python3,建立名为camera.py的新文件,注意,千万不能命名为picamera.py

2. 在文件中写入一下代码,运行即可,注意:远程连接(例如:SSH和VNC)时不允许访问摄像头


 


from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()

sleep(10)

camera.stop_preview()

3. 如果图像时上下颠倒的,可以用一下代码实现翻转

camera
rotation
camera
start_preview
sleep
camera
stop_preview

可以设置旋转的度数为90,180或270,也可以设置为0

4. 通过改变alpha的值,可以修改摄像头拍摄图像的透明度

picamera 
 PiCamera
time 
 sleep
camera 
 PiCamera
camera
start_preview
alpha
sleep
camera
stop_preview

alpha的取值范围为0-255

三.拍照片

1. 修改代码:减少sleep并添加camera.capture()一行

camera
start_preview
sleep
camera
capture
camera
stop_preview

捕获图片前,至少要给传感器两秒钟时间感光

2. 增加一个循环就能实现连拍,每隔5秒拍一张,拍完后预览关闭,桌面上就有五张图了

camera
start_preview
i 
 range
sleep
camera
capture
 
 i
camera
stop_preview

3. 查看图片:gpicview image1.jpg

四.拍视屏

1. 修改代码:用start_recording()和stop_recording()代替capture()

camera
start_preview
camera
start_recording
sleep
camera
stop_recording
camera
stop_preview

2. 运行程序,将拍摄10秒的视屏,然后关闭预览

3. 播放视屏需要执行如下指令:

omxplayer video
h264

4. 播放时可能会出现播放速度比实际速度快的情况,这是omxplayer的快速帧速率导致的

五.进阶教程

1. 摄像头的分辨率可以自己设定,但要记住,照片的最大分辨率时2592x1944,视频的最大分辨率为1920x1080,可以通过一下代码设定分辨率,为了配合最大分辨率,需要设置帧速率为15

camera
resolution
camera
framerate
camera
start_preview
sleep
camera
capture
camera
stop_preview

2. 最小分辨率允许设为64x64

3. 用annotate_text很方便地为图像添加文字

camera
start_preview
camera
annotate_text
sleep
camera
capture
camera
stop_preview

4. 改变亮度设置,范围是0-100,预设为50

camera
start_preview
camera
brightness
sleep
camera
capture
camera
stop_preview

5. 在循环中调整亮度,并标注亮度等级

camera
start_preview
i 
 range
camera
annotate_text 
 
 
 i
camera
brightness 
 i
sleep
camera
stop_preview

6. 调节文字大小,6-160,预设为32

camera
annotate_text_size

7. 改变文字的颜色,首先要引入Color,然后就能用如下代码实现

picamera 
 PiCamera
 Color
camera
start_preview
camera
annotate_background 
 Color
camera
annotate_foreground 
 Color
camera
annotate_text
sleep
camera
stop_preview

8. 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
sleep
camera
capture
camera
stop_preview

9. 用循环显示不同效果的预览

camera
start_preview
effect 
 camera
IMAGE_EFFECTS
camera
image_effect 
 effect
camera
annotate_text 
 
 
 effect
sleep
camera
stop_preview

10. camera.awb_mode可以设置白平衡,可选参数有:off,auto, sunlight, cloudy, shade, tungsten, fluorescent,incandescent, flash和horizon,预设为auto

camera
start_preview
camera
awb_mode
sleep
camera
capture
camera
stop_preview

可以用camera.AWB_MODES循环展示多个白平衡模式

11.  camera.exposure_mode可以设置曝光,可选参数有:off,auto,night,nightpreview,nightpreview,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks。预设是auto

camera
start_preview
camera
exposure_mode
sleep
camera
capture
camera
stop_preview

可以用camera.EXPOSURE_MODES循环展示多个曝光模式