读picamera 文档总结(一)

https://picamera.readthedocs.io/en/release-1.13/

这个包提供一个python接口来操作树莓派摄像头。

1. Installation

该软件包的安装

2. Getting Started

摄像头的安装以及简单的测试

3. Basic Recipes(都是捕获输出到文件或者流对象)

3.1. Capturing to a file

from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('foo.jpg')

Note that files opened by picamera (as in the case above) will be flushed and closed so that when the capture() method returns, the data should be accessible to other processes.

意思是被picamera打开的文件(foo.jpg)将会被刷新和关闭,以便于在capture()函数返回时,其他进程可以访问这个文件。

3.2. Capturing to a stream

from io import BytesIO
from time import sleep
from picamera import PiCamera

# Create an in-memory stream
my_stream = BytesIO()
camera = PiCamera()
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture(my_stream, 'jpeg')

Note that the format is explicitly specified in the case above. TheBytesIO object has no filename, so the camera can’t automaticallyfigure out what format to use.

捕获一张图片输出到流对象,像上面的BytesIO()。

One thing to bear in mind is that (unlike specifying a filename), the stream isnot automatically closed after capture; picamera assumes that since it didn’topen the stream it can’t presume to close it either. However, if the object hasa flush method, this will be called prior to capture returning. This shouldensure that once capture returns the data is accessible to other processesalthough the object still needs to be closed:One thing to bear in mind is that (unlike specifying a filename), the stream isnot automatically closed after capture; picamera assumes that since it didn’topen the stream it can’t presume to close it either. However, if the object hasa flush method, this will be called prior to capture returning. This shouldensure that once capture returns the data is accessible to other processesalthough the object still needs to be closed。

扫描二维码关注公众号,回复: 5034688 查看本文章

要注意的是:stream对象不会在调用capture方法后自动关闭。

3.3. Capturing to a PIL Image

from io import BytesIO
from time import sleep
from picamera import PiCamera
from PIL import Image

# Create the in-memory stream
stream = BytesIO()
camera = PiCamera()
camera.start_preview()
sleep(2)
camera.capture(stream, format='jpeg')
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
image = Image.open(stream)

This is a variation on Capturing to a stream. First we’ll capture an image to aBytesIO stream (Python’s in-memory stream class), then we’llrewind the position of the stream to the start, and read the stream into aPIL Image object。

这是上一个的变体,捕获到流对象以后,把这个对象倒一下(rewind),读取出来为PIL Image对象

3.4. Capturing resized images

from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('foo.jpg', resize=(320, 240))

Sometimes, particularly in scripts which will perform some sort of analysis orprocessing on images, you may wish to capture smaller images than the currentresolution of the camera. Although such resizing can be performed usinglibraries like PIL or OpenCV, it is considerably more efficient to have thePi’s GPU perform the resizing when capturing the image. This can be done withthe resize parameter of the capture() methods:

在capture函数中加入resize函数,可以调整图片的大小,这个是通过树莓派的GPU实现的,更为高效。

3.5. Capturing consistent images

from time import sleep
from picamera import PiCamera

camera = PiCamera(resolution=(1280, 720), framerate=30)
# Set ISO to the desired value
camera.iso = 100
# Wait for the automatic gain control to settle
sleep(2)
# Now fix the values
camera.shutter_speed = camera.exposure_speed
camera.exposure_mode = 'off'
g = camera.awb_gains
camera.awb_mode = 'off'
camera.awb_gains = g
# Finally, take several photos with the fixed settings
camera.capture_sequence(['image%02d.jpg' % i for i in range(10)])

You may wish to capture a sequence of images all of which look the same interms of brightness, color, and contrast (this can be useful in timelapsephotography, for example). Various attributes need to be used in order toensure consistency across multiple shots. Specifically, you need to ensure thatthe camera’s exposure time, white balance, and gains are all fixed:

用于捕获一系列图片,使用相同的参数,参数设置参考上面代码。

3.6. Capturing timelapse sequences

from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.start_preview()
sleep(2)
for filename in camera.capture_continuous('img{counter:03d}.jpg'):
    print('Captured %s' % filename)
    sleep(300) # wait 5 minutes

The simplest way to capture long time-lapse sequences is with thecapture_continuous() method. With this method, the cameracaptures images continually until you tell it to stop. Images are automaticallygiven unique names and you can easily control the delay between captures. Thefollowing example shows how to capture images with a 5 minute delay betweeneach shot:

间隔拍摄照片,可以设定时间间隔。



猜你喜欢

转载自blog.csdn.net/m0_37509650/article/details/80282646