A python program that resizes a video and turns it into an image

I couldn't find it when I looked for it, so I made it. Resize the video file that can be opened with OpenCV while thinning it out at appropriate frames and save it as an image file.

code

I haven't done anything difficult, so I don't think it's necessary to explain anything.

video2jpg.py


import cv2
import os
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--video_path", type=str)
parser.add_argument("--save_dir", type=str, default="save/")
parser.add_argument("--interval", type=int, default=10)
parser.add_argument("--resize", type=int, default=None, nargs="+")
opt = parser.parse_args()

def video2jpg():
    if not os.path.exists(opt.save_dir):
        os.makedirs(opt.save_dir)
    cap = cv2.VideoCapture(opt.video_path)
    l_cap = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 

    for i in range(l_cap):
        flag, frame = cap.read()
        if flag == True and i % opt.interval == 0: 
            save_path = os.path.join(opt.save_dir, "{}_{}.jpg ".format(os.path.basename(opt.video_path).split(".")[0], i))
            if opt.resize != None: 
                frame = cv2.resize(frame, (opt.resize[0], opt.resize[1]))  
            cv2.imwrite(save_path, frame)
            print("{} has been saved".format(save_path))

if __name__ == "__main__":
    video2jpg()

Run

For example, if you want to resize douga.avi, which exists in the same directory as video2jpg.py, to 480 x 480 every 10 frames and save it as an image file, enter the following in the terminal.

python3 video2jpg.py --video_path douga.avi --interval 10 --resize 480 480

Recommended Posts

A python program that resizes a video and turns it into an image
Let's write a Python program and run it
I want to exe and distribute a program that resizes images Python3 + pyinstaller
A Python program that converts ical data into text
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
A note that runs an external program in Python and parses the resulting line
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
Note that Python decodes base64 format image and saves it locally
[Python] A program that creates stairs with #
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
[Python] Create a linebot to write a name and age on an image
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
A program that automatically determines whether an animation or a photo is entered when a person's image is input [python]
Make a Python program a daemon and run it automatically when the OS starts
A Python program that checks if any content has been updated and git commits & pushes it to GitLab if it has been updated.
Generate an SSID and password, make it a QR code and throw it into Slack.
A nice nimporter that connects nim and python
[Python, PyPDF2] A script that divides a spread PDF into two left and right
ffmpeg-Build a python environment and split the video
Divide each PowerPoint slide into a JPG file and output it with python
A program that removes duplicate statements in Python
A program that searches for the same image
Crop Numpy.ndarray and save it as an image
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the number of updates of the highest and lowest records
A Python script that crawls RSS in Azure Status and posts it to Hipchat
A program that asks for a few kilograms to reach BMI and standard weight [Python]
I made a program to convert images into ASCII art with Python and OpenCV
I made a Docker Image that reads RSS and automatically tweets regularly and released it.
A script that combines your favorite python modules and binaries into one Lambda Layer
A memo that detects and returns an image acquired from a webcam with Django's OpenCV
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
[Python] A program that counts the number of valleys
Python that merges a lot of excel into one excel
[Python] Mask the image into a circle using Pillow
Python: I tried a liar and an honest tribe
[Python] A program that compares the positions of kangaroos.
A story that I wondered if it stopped working at mglearn.plots.plot_nmf_faces and was an error?
I tried "Streamlit" which turns the Python code into a web application as it is
A story that makes it easy to estimate the living area using Elasticsearch and Python
Steps to create a Job that pulls a Docker image and tests it with Github Actions
An easy way to view the time taken in Python and a smarter way to improve it
[Python] A program to find the number of apples and oranges that can be harvested
A Python program that collects tweets containing specific keywords daily and saves them in csv
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
Give a Python decorator an alias that passes fixed arguments
Temporarily save a Python object and reuse it in another Python
A Python script that saves a clipboard (GTK) image to a file.
Associate Python Enum with a function and make it Callable
Get a Python web page, character encode it, and display it
I tried "a program that removes duplicate statements in Python"
[Python] A program that creates a two-dimensional array by combining integers
Create code that outputs "A and pretending B" in python
A program that just presses and releases the Esc key
[Python] A program that finds the most common bird types
How to write a metaclass that supports both python2 and python3
A python beginner tried to intern at an IT company
A Python program that aggregates time usage from icalendar data