Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the simply-static domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/wp-includes/functions.php on line 6121
2022 年 4 月 – 第 2 页

light led

import RPi.GPIO as GPIO   #导入树莓派提供的python模块
import time   #导入时间包,用于控制闪烁
GPIO.setmode(GPIO.BCM)   #设置GPIO模式,BCM模式在所有数码派通用
GPIO.setup(26, GPIO.OUT)   #设置GPIO18为电流输出
while True:
    GPIO.output(26, GPIO.HIGH)   #GPIO18 输出3.3V
    time.sleep(1)   #程序控制流程睡眠0.05秒
    GPIO.output(26, GPIO.LOW)    #GPIO18 输出0V
    time.sleep(1)   #程序控制流程睡眠0.05秒

利用python将延时摄像图片自动合成为短视频

利用python实现延时摄像图片的视频化,也就是将图片合成为视频:

import cv2
import glob
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    for filename in glob.glob(path+'/*.jpg'):
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 5
    out = cv2.VideoWriter('/Users/Downloads/中转文件夹/2022-02-20-03.jpg/demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/Users/Downloads/中转文件夹/2022-02-20-03.jpg/"
    images_to_video(path)
if __name__ == "__main__":
    main()

以上代码可能会重复将jpg文件编入视频,改进如下:

import cv2
import glob
import re
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    jpgfile=glob.glob(path+'/*.jpg')
    ordjpgfile=sorted(jpgfile,key = str .lower)
    for filename in ordjpgfile:
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 10
    out = cv2.VideoWriter('/Users/Downloads/2022/demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/Users/Downloads/2022/"
    images_to_video(path)
if __name__ == "__main__":
    main()

自动根据时间生成文件名

import cv2
import glob
import re
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    jpgfile=glob.glob(path+'/*.jpg')
    ordjpgfile=sorted(jpgfile,key = str .lower)
    for filename in ordjpgfile:
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    aviname=str(ordjpgfile[0])[-23:-13]+'.avi'
    print(aviname)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 10
    out = cv2.VideoWriter('/home/peter/2022/'+aviname, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/home/peter/2022"
    images_to_video(path)
if __name__ == "__main__":
    main()