[Python] Save the video data imported by OpenCV as a serial number jpg file

To be honest, I think it's better to look for free tools. In an environment where you can't put free tools in your work or detour I make something similar every time, so I wrote it as a memorandum.

Video source as the first argument. → When accessing the webcam, specify the number 0-9. When accessing a video file, specify the video file with a relative or absolute path. The frame rate is the second argument. → Frame rate when displaying. Omission processing that only changes the waiting time of cv2.waitKey (). The reduction ratio is the third argument. → Specify when changing the size of the jpg file to be spit out. If you specify 2, the image will be 1/2 the width and height.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
video2jpg.py.

Usage:
  video2jpg.py [<video source>] [<fps>] [<resize_rate>]
'''

import numpy as np
import cv2
import sys

print(__doc__)

try:
    fn = sys.argv[1]
    if fn.isdigit() == True:
        fn = int(fn)
except:
    fn = 0
print fn

try:
    fps = sys.argv[2]
    fps = int(fps)
except:
    fps = 30
print fps

try:
    resize_rate = sys.argv[3]
    resize_rate = int(resize_rate)
except:
    resize_rate = 1
print resize_rate

video_input = cv2.VideoCapture(fn)
if (video_input.isOpened() == False):
    exit()

count = 0
while(True):
    count += 1
    count_padded = '%05d' % count

    ret, frame = video_input.read()

    height, width = frame.shape[:2]
    small_frame = cv2.resize(frame, (int(width/resize_rate), int(height/resize_rate)))

    cv2.imshow('frame', small_frame)
    c = cv2.waitKey(int(1000/fps)) & 0xFF

    write_file_name = count_padded + ".jpg "
    cv2.imwrite(write_file_name, small_frame)

    if c==27: # ESC
        break

video_input.release()
cv2.destroyAllWindows()

Recommended Posts

[Python] Save the video data imported by OpenCV as a serial number jpg file
[python] Change the image file name to a serial number
Save video frame by frame with Python OpenCV
[Python] You can save an object to a file by using the pickle module.
Save the result of the life game as a gif with python
Save the binary file in Python
[Python] How to store a csv file as one-dimensional array data
Get the formula in an excel file as a string in Python
A memo organized by renaming the file names in the folder with python
[Bash] Command to write a file line by line to a serial number file [One liner (?)]
Read the file line by line in Python
Read the file line by line in Python
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Save the setting conditions as a CSV file using UDF Manager in OCTA
Python --Read data from a numeric data file and find the multiple regression line.
[Python] Plot data by prefecture on a map (number of cars owned nationwide)
ffmpeg-Build a python environment and split the video
Save the object to a file with pickle
Export Python3 version OpenCV KeyPoint to a file
Read line by line from a file with Python
Sort Fashion-MNIST data and save as PNG file
The file name saved by pysheng was a hexadecimal number, so I fixed it.
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
Check if the string is a number in python
The avi file output by OpenCV cannot be played
[Python] A program that counts the number of valleys
Read the xml file by referring to the Python tutorial
Fill the background with a single color with OpenCV2 + Python
How to save a table scraped by python to csv
Output the output result of sklearn.metrics.classification_report as a CSV file
Python> __init__.py> Required to handle the specified directory as a package (empty file is acceptable)