1
0

More than 1 year has passed since last update.

連番画像を動画にするためのプログラム

Posted at

連番画像を動画にするためのプログラム

自分のPCではシーケンサーからの出力をaviとして得るとなんかきもかったので,連番画像として出力したのち,いい感じのものだったら動画に変換した.
実行例は,

$ python3 conv.py d1 1800

自分の場合シーンごとにシーケンサと名前を変えていたのでこんな感じに
入力としてほしい画像の名前は,imgファイルにあるren_d1.0001.jpg....ren_d1.1799.jpgのような感じ
出力される動画はmovies_aftフォルダ内にren_d1.mp4のように保存される
お好みで変えてもらえればいいと思います

├ conv.py
├ img/
│  ├ ren_d1.0001.jpg
│  │     ・
│  │     ・
│  │     ・
│  └ ren_d1.1799.jpg
└ movies_aft/
   └ ren_d1.mp4 →これが作られる
conv.py
import cv2
import numpy as np
import time
import sys

def main():

    args = sys.argv
    try:
        stage=args[1]
        mcount = int(args[2])
        print(args[0],type(stage),stage,type(mcount),mcount)

    except IndexError as e:
        print(e)

    origpath = "img/"
    imname = f"ren_{stage}.NUM.jpg"

    startTime = time.time()
    for i in [0]:
        num = str(i).zfill(4)
        print(num+"  ",end="")
        print(type(num))
    fIm= cv2.imread(origpath+imname.replace("NUM",num))
    if not fIm is None:
        print("pic is arive")
    else:
        print("yes")
        return
    # cv2.imwrite("uni.png",fIm)
    img_last = None

    frame_rate = 30.0 # フレームレート
    # 幅と高さを取得
    imShape = fIm.shape
    size = (imShape[1],imShape[0])
    print(size)

    codec = cv2.VideoWriter_fourcc(*'mp4v') # ファイル形式(ここではmp4)
    print(f'movies_aft/ren_{stage}.mp4')#
    writer = cv2.VideoWriter(f'movies_aft/ren_{stage}.mp4', codec, frame_rate, size) # ライター作成

    for i in range(30,mcount,2):
        deltime = time.time() - startTime
        if i!=0:
            print(f'\r{str(i).rjust(4)}/{mcount}  予想時間残り:{str(int((mcount-i)*deltime/i)).rjust(4)}s',end='')

        num = str(i).zfill(4)

        fIm= cv2.imread(origpath+imname.replace("NUM",num))
        writer.write(fIm)
    # endfor

    writer.release()
    print("\ncomplete!!!")
    return
#end main

if __name__=='__main__':
    main()
# endif

参考

いろいろと参考にしましたが忘れました
すみません
特に,コマンドラインでの処理経過を残す部分ができて個人的には満足しています
わからないだれかありがとうございました

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0