Home >Backend Development >Python Tutorial >How to merge multiple pictures into mp4 videos based on Python
Python module imageio is a library for reading and writing image and video data. It supports a variety of formats and can operate with NumPy arrays.
The following are some key functions:
imageio.imread(): Read image data from the file and return a NumPy array.
imageio.imwrite(): Writes a NumPy array to a file.
imageio.mimsave(): Save a list of multiple image frames as an animation file (such as GIF).
imageio.get_reader(): Gets an object used to read the animated image in the given file frame by frame.
imageio.get_writer(): Gets an object that is used to write animated images to the specified file frame by frame.
Using these functions, you can easily read, write and process various image and video data formats.
PIL is a commonly used image processing library in Python, and the Image module is an important module. It provides various image operation and processing functions.
The following are some main functions and their functions in the Image module:
open(): Open and return the image object of the specified file path.
fromarray(): Convert a numpy array or PIL image object to a PIL image object.
new(): Create a new blank image object of specified size, mode and color.
save(): Save the image as a file in the specified format.
resize(): Modify the size of the image.
crop(): Crop the specified area of the image.
rotate(): Rotate the image by a specified angle.
show(): Display the image.
In addition, there are many other commonly used functions, such as adjusting brightness and contrast, applying filters, etc. These functions can help us implement various complex image processing tasks, and there are usually some parameters that can be used to further adjust the behavior of the function. You can refer to the relevant documentation for specific use.
Use PIL (Python Imaging Library) or OpenCV in Python to read each picture.
Use the third-party library imageio to merge multiple pictures into a video file.
Configure parameters such as the number of video frames, video size, and video playback speed of the generated video file.
The following is a simple sample code:
import os import imageio from PIL import Image # 设置生成的视频文件名和路径 filename = 'output.mp4' filepath = os.path.join(os.getcwd(), filename) # 读取所有 PNG 图片 images = [] for file_name in sorted(os.listdir()): if file_name.endswith('.png'): images.append(Image.open(file_name)) # 将图片转换为视频 fps = 30 # 每秒钟30帧 with imageio.get_writer(filepath, fps=fps) as video: for image in images: frame = image.convert('RGB') video.append_data(frame)
Implementing the principle, read all the image files that need to be merged and store them in a list. Then use the get_writer() function of the imageio library to create a video writer object and set the playback speed (fps) of the video. Then during the loop, each image in the list is read and added to the video frame after converting it to a format with RGB color mode. The final output is a video file that combines all target images.
Parallelize the program to speed up the processing of multiple pictures. Note: The following method may not be the optimal method
import os import concurrent.futures import imageio from PIL import Image # 设置生成的视频文件名和路径 filename = "output.mp4" filepath = os.path.join(os.getcwd(), filename) def process_image(file_name): if file_name.endswith(".png"): image = Image.open(file_name) return image.convert("RGB") with concurrent.futures.ThreadPoolExecutor() as executor: # 寻找所有 png 文件 image_files = [file for file in os.listdir() if file.endswith(".png")] # 利用线程池并行处理图像 images = list(executor.map(process_image, image_files)) # 将图片转换为视频文件 fps = 30 # 每秒钟30帧 with imageio.get_writer(filepath, fps=fps) as video: for image in images: video.append_data(image)
The ThreadPoolExecutor object in the concurrent.futures library is used here for parallel processing. First, in the main thread, find and read all the PNG images that need to be merged in the current working directory, and create a task list. Then submit the task to the thread pool and ensure that the target file can be processed quickly and asynchronously. After all PNG images are processed, they are stored into a list in images and finally converted into videos.
The above is the detailed content of How to merge multiple pictures into mp4 videos based on Python. For more information, please follow other related articles on the PHP Chinese website!