python 视频加滤镜 moviepy opencv

使用moviepy给视频加上滤镜

        def cyberpunk(image):
            # 反转色相
            image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
            image_hls = np.asarray(image_hls, np.float32)
            hue = image_hls[:, :, 0]
            hue[hue < 90] = 180 - hue[hue < 90]
            image_hls[:, :, 0] = hue

            image_hls = np.asarray(image_hls, np.uint8)
            image = cv2.cvtColor(image_hls, cv2.COLOR_HLS2BGR)

            image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
            image_lab = np.asarray(image_lab, np.float32)

            # 提高像素亮度,让亮的地方更亮
            light_gamma_high = np.power(image_lab[:, :, 0], 0.8)
            light_gamma_high = np.asarray(light_gamma_high / np.max(light_gamma_high) * 255, np.uint8)

            # 降低像素亮度,让暗的地方更暗
            light_gamma_low = np.power(image_lab[:, :, 0], 1.2)
            light_gamma_low = np.asarray(light_gamma_low / np.max(light_gamma_low) * 255, np.uint8)

            # 调色至偏紫
            dark_b = image_lab[:, :, 2] * (light_gamma_low / 255) * 0.1
            dark_a = image_lab[:, :, 2] * (1 - light_gamma_high / 255) * 0.3

            image_lab[:, :, 2] = np.clip(image_lab[:, :, 2] - dark_b, 0, 255)
            image_lab[:, :, 2] = np.clip(image_lab[:, :, 2] - dark_a, 0, 255)

            image_lab = np.asarray(image_lab, np.uint8)
            # 注意视频存在色差,更改颜色模式为COLOR_Lab2RGB
            return cv2.cvtColor(image_lab, cv2.COLOR_Lab2RGB)

        # 视频帧加入滤镜
        modifiedClip = out_video.fl_image(cyberpunk)

 参考文档:Python 图像处理:滤镜之赛博朋克 - 简书

你可能感兴趣的:(Python,python)