Skip to content

Commit 7217d1c

Browse files
committed
add creating video from images tutorial
1 parent 45ba1f8 commit 7217d1c

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
267267
- [How to Add Audio to Video in Python](https://www.thepythoncode.com/article/add-audio-to-video-in-python). ([code](python-for-multimedia/add-audio-to-video))
268268
- [How to Compress Images in Python](https://www.thepythoncode.com/article/compress-images-in-python). ([code](python-for-multimedia/compress-image))
269269
- [How to Remove Metadata from an Image in Python](https://thepythoncode.com/article/how-to-clear-image-metadata-in-python). ([code](python-for-multimedia/remove-metadata-from-images))
270+
- [How to Create Videos from Images in Python](https://thepythoncode.com/article/create-a-video-from-images-opencv-python). ([code](python-for-multimedia/create-video-from-images))
270271

271272
- ### [Web Programming](https://www.thepythoncode.com/topic/web-programming)
272273
- [Detecting Fraudulent Transactions in a Streaming Application using Kafka in Python](https://www.thepythoncode.com/article/detect-fraudulent-transactions-with-apache-kafka-in-python). ([code](general/detect-fraudulent-transactions))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create Videos from Images in Python](https://thepythoncode.com/article/create-a-video-from-images-opencv-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import cv2
2+
import argparse
3+
import glob
4+
from pathlib import Path
5+
import shutil
6+
7+
# Create an ArgumentParser object to handle command-line arguments
8+
parser = argparse.ArgumentParser(description='Create a video from a set of images')
9+
10+
# Define the command-line arguments
11+
parser.add_argument('output', type=str, help='Output path for video file')
12+
parser.add_argument('input', nargs='+', type=str, help='Glob pattern for input images')
13+
parser.add_argument('-fps', type=int, help='FPS for video file', default=24)
14+
15+
# Parse the command-line arguments
16+
args = parser.parse_args()
17+
18+
# Create a list of all the input image files
19+
FILES = []
20+
for i in args.input:
21+
FILES += glob.glob(i)
22+
23+
# Get the filename from the output path
24+
filename = Path(args.output).name
25+
print(f'Creating video "{filename}" from images "{FILES}"')
26+
27+
# Load the first image to get the frame size
28+
frame = cv2.imread(FILES[0])
29+
height, width, layers = frame.shape
30+
31+
# Create a VideoWriter object to write the video file
32+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
33+
video = cv2.VideoWriter(filename=filename, fourcc=fourcc, fps=args.fps, frameSize=(width, height))
34+
35+
# Loop through the input images and add them to the video
36+
for image_path in FILES:
37+
print(f'Adding image "{image_path}" to video "{args.output}"... ')
38+
video.write(cv2.imread(image_path))
39+
40+
# Release the VideoWriter and move the output file to the specified location
41+
cv2.destroyAllWindows()
42+
video.release()
43+
shutil.move(filename, args.output)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

0 commit comments

Comments
 (0)