SoFunction
Updated on 2025-03-01

Python implementation to make long pictures into a video

How to make a long picture into a video in Python

To make a long picture into a video, you can use some libraries in Python to accomplish this task. Here is a basic approach, using the PIL (Python Imaging Library) library to process images, and the opencv-python library to create videos.

First, make sure you have installed the PIL and opencv-python libraries. You can install them via pip using the following command:

pip install Pillow
pip install opencv-python

Next, you need to prepare a long picture, and some parameters to specify the frame rate of the video, the name of the output video, etc.

Here is a sample code that divides a long image into a series of frames and saves these frames as image files, which then merges these image files into a video:

from PIL import Image
import cv2
import os
def create_video_from_image(image_path, output_video_path, fps):
    # Open the long picture    image = (image_path)
    width, height = 
    # Calculate the size of the video    video_width = width
    video_height = height
    # Create a video writer    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = (output_video_path, fourcc, fps, (video_width, video_height))
    # Synthesize the long picture as a frame and add each frame to the video    frame_index = 0
    while True:
        try:
            # Calculate the position of the current frame in the long graph            x = 0
            y = frame_index * video_height
            # Cut frames            frame = ((x, y, x + video_width, y + video_height))
            # Convert PIL image to OpenCV image            frame = ((frame), cv2.COLOR_RGB2BGR)
            # Write to video            video_writer.write(frame)
            frame_index += 1
        except:
            break
    # Free up resources    video_writer.release()
    print(f"The video has been saved as {output_video_path}")
# Example usageimage_path = "Long Picture.jpg"  # Long map pathoutput_video_path = "Long Picture Video.mp4"  # Output video pathfps = 24  # Video frame ratecreate_video_from_image(image_path, output_video_path, fps)

Make sure to replace the long picture.jpg in the above code with your actual long picture file path and specify the path and frame rate of the output video. Then, running the code will generate a video file where each frame is cut from the long picture.

Note that this method assumes that the height of the long graph is an integer multiple of the video and that the width of each frame is the same as the long graph. If the size of the long diagram does not match these requirements, you may need to make some adjustments to suit your specific situation.

Knowledge Supplement

Of course, in addition to making long pictures into videos, Python can also generate long pictures based on text. The following is the implementation code. I hope everyone likes it

Python generates growth graphs based on a paragraph of text

To generate a long picture based on the provided text, you can usePIL(Python Imaging Library) library to create images, andImageDrawModule to draw text.

Here is a sample code that draws the given text line by line onto a long picture and saves it as an image file:

from PIL import Image, ImageDraw, ImageFont
# Define the width and height of the long graphimage_width = 800
line_height = 40
padding = 10
# Define a text listtext_list = [
    "1. Super fun C++ console airplane game with source code",
    "++Facing the object(Down)",
    "3.【C++from0Go to the king】Stop 6:Classes and Objects(Down)",
    "++:EffectiveC++:Article01:WillC++Treat as a language federation",
    "5.learn_C_deep_14 (Basic use and understanding of conditional compilation)",
    "6.Lexical analysis experiment of compilation principle(Attached completeC/C++Code and summary)"
]
# Calculate the height of the long graphimage_height = (line_height + padding) * len(text_list) + padding
# Create blank imageimage = ("RGB", (image_width, image_height), (255, 255, 255))
draw = (image)
# Set font style and sizefont = ("", 20)
# Draw text line by linefor i, text in enumerate(text_list):
    y = i * (line_height + padding) + padding
    ((padding, y), text, font=font, fill=(0, 0, 0))
# Save the image("Long Picture.jpg")
print("Long picture has been generated and saved as long picture.jpg")

Please make sure to include the above code in theReplace with the path of the font file that actually exists on your computer. Then, running the code will generate an image file called "Long Picture.jpg" that contains the given text content.

You can adjust the parameters such as width, line height, font style of the long picture as needed to meet your specific needs.

This is the end of this article about Python implementing making long pictures into a video. For more related content on Python video production, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!