gif --- gif 录制¶
gif 模块用于 gif 录制。
class Gif -- Gif 录制器¶
你可以使用 gif 模块录制小段视频片段。请注意,gif 文件保存的是未压缩的图像数据。因此,它们最适合录制你想要分享的短视频片段。对于长片段,请使用 mjpeg。
用法示例:
import csi
import gif
import time
# Setup camera.
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.GRAYSCALE)
csi0.framesize(csi.QQVGA)
csi0.snapshot(time=2000)
# Create the gif object.
g = gif.Gif("example.gif")
# Record for 4 seconds.
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < 4000:
g.add_frame(csi0.snapshot())
# Finalize.
g.close()
- class gif.Gif(filename: str, width: int | None = None, height: int | None = None, color: bool | None = None, loop: bool = True)¶
创建一个可向其中添加帧的 Gif 对象。
filename是保存 gif 录制内容的路径。width默认为主帧缓冲区的水平分辨率。height默认为主帧缓冲区的垂直分辨率。color默认为主帧缓冲区的颜色模式:False 生成每像素 7 位的
sensor.GRAYSCALEgif。True 生成每像素 7 位的
sensor.RGB565gif。
loop为 True 时,gif 在播放时会自动循环。- format() int¶
如果 color 为 True,则返回
sensor.RGB565,否则返回sensor.GRAYSCALE。
- add_frame(image: image.Image, delay: int = 10) None¶
向 gif 录制中添加一张图像。图像的宽度、高度和颜色模式必须与构造函数中使用的值一致。
delay是在显示这一帧之前相对上一帧需要等待的厘秒数。