圖片轉視訊
阿新 • • 發佈:2018-12-22
依賴
- Python
- OpenCV
首先讀取圖片名稱,並寫入到txt檔案,指令碼get_filename.sh :
for i in $(ls $1)
do
echo ${i}
done
在終端執行:
chmod +x /get_filename.sh
./get_filename.sh ~/images > ~/filename.txt
圖片轉視訊的 image2video.py:
import cv2 import os im_dir = '~/images' #圖片路徑 video_dir = '~/output.avi' #視訊路徑,一般是.avi格式 fps = 10 img_size = (1226,370) fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv2.4 #fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0 videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size) fr = open('~/filename.txt','r') #儲存圖片名稱的txt檔案 for im_name in fr: im_name = im_name.strip('\n') im_file = os.path.join(im_dir, im_name) im = cv2.imread(im_file) videoWriter.write(im) print im_name videoWriter.release() print 'finish'
最後在終端執行以下命令即可得到視訊檔案:
python image2video.py