python 读取视频帧,得到图片(一)

方法一:通过imageio库和skimage库
  1. 安装环境:
    pip install imageio
    pip install skimage
    这时候会报错

Please install the scikit-imagepackage (instead of skimage)

所以按照提示操作即可:
pip install scikit-image
环境安装成功
2.通过python安装ffmpge(用来解码视频流)
import imageio
imageio.plugins.ffmpeg.download()
这时候会提示:

Imageio: 'ffmpeg.linux64' was not found on your computer; downloading it now.Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg.linux64 (27.2 MB)Downloading: 8192/28549024 bytes

  1. 使用imageio库提取视频帧
# coding:utf-8
import pylab
import imageio
#注释的代码执行一次就好,以后都会默认下载完成
#imageio.plugins.ffmpeg.download()
import skimage
import numpy as np
#视频的绝对路径
filename = 'video/01_01.wmv'
#可以选择解码工具
vid = imageio.get_reader(filename, 'ffmpeg')
for num,im in enumerate(vid):
#image的类型是mageio.core.util.Image可用下面这一注释行转换为arrary
print im.mean()
image = skimage.img_as_float(im).astype(np.float64)
fig = pylab.figure()
fig.suptitle('image #{}'.format(num), fontsize=20)
pylab.imshow(im)
pylab.show()

这是自己写的一个(有临时文件夹产生 TemporaryDirectory()

def _parse_thzcm_document(self, document_id, only_preview_fields):
        import imageio, os
        from PIL import Image
        from tempfile import TemporaryDirectory
        file = self.env['muk_dms.file'].search([('id', '=', document_id)])
        vid = imageio.get_reader(file.content_file, 'ffmpeg')
        if file:
            # 'C:\\Users\\test\\AppData\\Local\\Temp\\tmp8z_h3vd_'
            with TemporaryDirectory() as temp_folder:
                for num, im in enumerate(vid):
                    Image.fromarray(im).save(temp_folder + '\\test.jpg')
                    image_id = tools.image_resize_image_big(base64.b64encode(open(temp_folder + '/test.jpg', 'rb').read()))
                    # path_pic = '%s' + '\\' + 'test.jpg' % (temp_folder)
                    # path_pic = os.path.join(temp_folder, 'test.jpg')
                    # image_id = os.path.basename(path_pic)
                    values = {'slide_type': 'video', 'image': image_id, 'document_id': document_id}
                    return {'values': values}
                else:
                    values = {'document_id': document_id}
                    return {'values': values}
        else:
            return {'error': _('Unknown document')}

看完记得点赞哦,笔芯

你可能感兴趣的:(python 读取视频帧,得到图片(一))