基于python-opencv和PIL的截取视频区域生成 GIF 动图

前言

​ 很多时候,我们想在文章中加入一些录屏的视频文件,可是一般都是太大不支持上传。gif无疑是更好的选择,可是市面上的软件要么收费,不收费的又大多都带水印。

​ 于是就有了这篇文章,我们自己写一个可截取视频区域并转换成gif文件的demo。

  • opencv读取视频

import cv2 as cv
video_path = r'./PCA_Program.mp4' # 视频文件路径
cap = cv.VideoCapture(video_path) # 实例化,读取视频
# fps = cap.get(cv.CAP_PROP_FPS) # 获取视频的帧率
# total_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT)) # 视频的总帧数
# image_size = (int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv.CAP_PROP_FRAME_WIDTH))) # 获取图像尺寸
while True:
    sucess, frame = cap.read()
    cv.imshow('frame', frame)
    key=cv.waitKey(10)
    if key == 27:
        cv.destroyAllWindows()
        break

​ 上面这段程序可以打开并播放我们指定路径的视频。


  • 截取要生成gif的区域

    利用cv.setMouseCallback函数,获取鼠标操作返回的像素坐标值。

def on_EVENT_LBUTTON(event, x, y, flags, param):  # 它本身就相当于已经在一个while里了
    global WN, ES # 视频选取区域的左上角和右下角(这里用东南西北的英文首字母表示)
    pic = copy.deepcopy(image)
    if event == cv.EVENT_LBUTTONDOWN:
        WN = (x, y)
        cv.circle(image, WN, 2, (0, 0, 255), 2)
        cv.imshow('image', image)
    elif event == cv.EVENT_MOUSEMOVE and (flags & cv.EVENT_FLAG_LBUTTON):
        cv.imshow('image', image)
    elif event == cv.EVENT_LBUTTONUP:
        ES = (x, y)
        cv.rectangle(image, WN, ES, (0, 0, 255), 2)
        cv.imshow('image', image)
        c = cv.waitKey(0)
        if c == 27:
            cv.destroyAllWindows()
            coord.append(WN)
            coord.append(ES)
            print(WN, ES)
            pic = pic[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
            cv.imshow('pic', pic)
            cv.waitKey(2000)
            
cv.imshow('image', image)
cv.setMouseCallback("image", on_EVENT_LBUTTON)
cv.waitKey(0)
cv.destroyAllWindows()

​ 上述代码可实现视频感兴趣区域的截取,输入为播放视频时,截取的那一帧图像,我们对该图像进行生成gif区域的截取。


  • 播放区域视频并选择开始帧和结束帧

# 存储截取区域帧图
gifs = []
cap = cv.VideoCapture(video_path)
for i in range(total_frames):
    sucess, frame = cap.read()
    single_frame = frame[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
    cv.imshow('frame', single_frame)
    c = cv.waitKey(1)
    if c == ord('s'): # 等待输入按键's',为开始帧
        start = i
    elif c == ord('e'): # 等待输入按键'e',为结束帧
        end = i
        cv.destroyAllWindows()
        break
    elif i == total_frames - 1: # 若无结束帧按键'e'输入,则默认到视频最后一帧结束
        end = total_frames 
        cv.destroyAllWindows() # 结束后关闭所有窗口

上述代码实现了开始帧和结束帧的选择,以上准备工作就绪后,我们就可以开始gif的生成工作了。


  • 存储区域截图并用PIL.Image生成gif文件

from PIL import Image
print('开始抽帧...')
cap = cv.VideoCapture(video_path)
for i in range(total_frames):
    sucess, frame = cap.read()
    single_frame = frame[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
    # 图像帧的缩放,为了压缩空间
    resizeimgAR = cv.resize(single_frame, None, fx=0.5, fy=0.5, interpolation=cv.INTER_AREA)
    frames = Image.fromarray(resizeimgAR)
    if start <= i <= end:
        gifs.append(frames)
print('开始生成gif...')
gifs[0].save(r'./PCA.gif', format='GIF', append_images=gifs[1::10],save_all=True, duration=1, loop=0)
print('gif生成成功...')

  • demo源码:

# -*- coding:utf-8 -*- #
"""
作者:魚香肉丝盖饭
日期:2022年04月01日
"""

"""
程序运行后,会直接播放视频
按空格键后,会停留在当前帧,等待鼠标画框选择生成gif的图像区域,显示截取的区域
按esc键后会关闭当前显示的区域,播放截取区域的视频
此时按s键,即选定开始帧,按e键,选定结束帧
按s键后不按e键,即默认视频最后一帧为结束帧
按e键或视频播放结束后,自动生成gif到视频目录的同文件夹下
"""

import cv2 as cv
from PIL import Image
import copy
import os

global image

coord = []


# 鼠标点击事件
def on_EVENT_LBUTTON(event, x, y, flags, param):  # 它本身就相当于已经在一个while里了
    global WN, ES
    pic = copy.deepcopy(image)
    if event == cv.EVENT_LBUTTONDOWN:
        WN = (x, y)
        cv.circle(image, WN, 2, (0, 0, 255), 2)
        cv.imshow('image', image)
    elif event == cv.EVENT_MOUSEMOVE and (flags & cv.EVENT_FLAG_LBUTTON):
        cv.imshow('image', image)
    elif event == cv.EVENT_LBUTTONUP:
        ES = (x, y)
        cv.rectangle(image, WN, ES, (0, 0, 255), 2)
        cv.imshow('image', image)
        c = cv.waitKey(0)
        if c == 27:
            cv.destroyAllWindows()
            coord.append(WN)
            coord.append(ES)
            print(WN, ES)
            pic = pic[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
            cv.imshow('pic', pic)
            cv.waitKey(2000)


video_path = r'.\CoppeliaSim Edu - RRRR_Sim_Kintmatics - rendering_ 5 ms (40.0 ' \
             r'fps) - SIMULATION RUNNING 2021-11-29 21-34-42.mp4 '
cap = cv.VideoCapture(video_path)
fps = cap.get(cv.CAP_PROP_FPS)
# 视频总帧数
total_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
# 图像尺寸
image_size = (int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv.CAP_PROP_FRAME_WIDTH)))

# 选取截取区域
for i in range(total_frames):
    sucess, frame = cap.read()
    # img = Image.fromarray(frame)
    cv.putText(frame, 'real_frames:' + str(i), (100, 200), cv.FONT_HERSHEY_SIMPLEX, 1, [255, 0, 0],
               thickness=3)
    cv.imshow('frame', frame)
    c = cv.waitKey(30)
    if c == ord(' '):  # esc按键的ASCII码为27,关闭窗口的逻辑
        image = frame[:, :]
        cv.destroyAllWindows()
        break
cv.imshow('image', image)
cv.setMouseCallback("image", on_EVENT_LBUTTON)
cv.waitKey(0)
cv.destroyAllWindows()

# 存储截取区域帧图
gifs = []
cap = cv.VideoCapture(video_path)
for i in range(total_frames):
    sucess, frame = cap.read()
    single_frame = frame[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
    cv.imshow('frame', single_frame)
    c = cv.waitKey(50)
    if c == ord('s'):
        start = i
    elif c == ord('e'):
        end = i
        cv.destroyAllWindows()
        break
    elif i == total_frames - 1:
        end = total_frames
        cv.destroyAllWindows()

print('开始抽帧...')
cap = cv.VideoCapture(video_path)
for i in range(total_frames):
    sucess, frame = cap.read()
    single_frame = frame[coord[0][1]:coord[1][1], coord[0][0]:coord[1][0]]
    resizeimgAR = cv.resize(single_frame, None, fx=0.8, fy=0.8, interpolation=cv.INTER_AREA)
    frames = Image.fromarray(resizeimgAR)
    if start <= i <= end:
        gifs.append(frames)
print('开始生成gif...')
gifs[0].save(
    r'.\CoppeliaSim Edu - RRRR_Sim_Kintmatics - rendering_ 5 ms (40.0 fps) - '
    r'SIMULATION RUNNING 2021-11-29 21-34-42.gif',
    format='GIF', append_images=gifs[1::6],
    save_all=True, duration=1, loop=0)
print('gif生成成功...')

demo源码给大家参考,后续会持续改进逻辑,并为其设计一个UI。

  • 实现效果:

这里选用视频是本人设计的机械臂,在CoppeliaSim中的机械臂正运动学录屏。

CSDN_video2gif演示视频

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值