Convert video to black and white with ffmpeg + python + opencv

Recently, I've been playing around with images taken from videos with the combination of "ffmpeg + python + opencv". This time, I will try to use these to "convert color videos to black and white".

Execution environment

OS: Windows7(Cygwin) ffmpeg: N-74313-g9c0407e Python: 2.7.10 OpenCV: 2.4.11

0. Work preparation

Create a working folder and place the original video file.

$ mkdir bwmovie/row bwmovie/bw
$ mv movie bwmovie/
$ cd bwmovie

1. Cut out a still image from a video

Move to the working folder and use ffmpeg to extract still images frame by frame from the video file.

$ ffmpeg -i movie -f image2 row/%06d.jpg

The last argument is the file name. In this case, the file name is given in the row folder so that it is "arranged in order from the one extracted with a 6-digit integer packed with 0s".

2. Convert still images to black and white

This conversion is done in Python + OpenCV. Create the following python code directly under the working folder.

import cv2
import glob

paths = glob.glob('row/*')

for index,path in enumerate(paths):
        image = cv2.imread(path, 0)
        ret, th = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU )

        cv2.imwrite('bw/%06d.jpg' % index, th)

What this code is doing is first get the files under the row folder as an array with paths = glob.glob ('row / *'). Then turn these file paths in a for loop and store them in image with ʻimage = cv2.imread (path, 0). At this time, 0 is specified for the second argument of imread so that it becomes grayscale. After that, binarize (black and white) the image with ret, th = cv2.threshold (image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) and cv2.imwrite ('bw /% 06d.jpg) Save it as a serial number in the image bw folder with'% index, th) `.

3. Stitch still images into a video

Finally, join the images converted to black and white to make a video.

 ffmpeg -i bw/%06d.jpg video.mp4

Specify the original image after the i option, and specify the output file name in the last argument.

Summary

With the above, you can "convert the video to black and white". However, the video output by this method has no audio and the bit rate is not specified, so the number of frames will increase compared to the original video and it will be a little dull. If you are a Kininal, please try google with "How to use ffmpeg".

Recommended Posts

Convert video to black and white with ffmpeg + python + opencv
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
Add fake tilt shift effect to video with OpenCV + Python
I made a program to convert images into ASCII art with Python and OpenCV
How to make a surveillance camera (Security Camera) with Opencv and Python
[Python] Try to recognize characters from images with OpenCV and pyocr
Convert memo at once with Python 2to3
Save video frame by frame with Python OpenCV
Capturing images with Pupil, python and OpenCV
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
Convert FX 1-minute data to 5-minute data with Python
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
Put OpenCV in OS X with Homebrew and input / output video with python
I made a network to convert black and white images to color images (pix2pix)
Read CSV file with Python and convert it to DataFrame as it is
Try to separate the background and moving object of the video with OpenCV
Hello World and face detection with OpenCV 4.3 + Python
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Convert DICOM to PNG with Ascending and Descending
Convert PDF to image (JPEG / PNG) with Python
Convert PDFs to images in bulk with Python
Convert mp4 to mp3 with ffmpeg (thumbnail embedded version)
Convert svg file to png / ico with Python
How to crop an image with Python + OpenCV
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Convert Windows epoch values to date with python
How to convert SVG to PDF and PNG [Python]
The easiest way to use OpenCV with python
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
[Python] How to handle Japanese characters with openCV
Try to detect fish with python + OpenCV2.4 (unfinished)
Convert strings to character-by-character list format with python
Convert 202003 to 2020-03 with pandas
Binarization with OpenCV / Python
Introduction to OpenCV (python)-(2)
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
Procedure to load MNIST with python and output to png
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[Python] Convert general-purpose container and class to each other
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Display and shoot webcam video with Python Kivy [GUI]
[Python] Webcam frame size and FPS settings with OpenCV
Reading, displaying and speeding up gifs with python [OpenCV]
Something to enjoy with Prim Pro (X-Play) and Python
Display USB camera video with Python OpenCV with Raspberry Pi
[Python] Convert time display (str type) using "" "and"'" to seconds (float type) with datetime and timedelta
Easy to use Nifty Cloud API with botocore and python
Programming with Python and Tkinter
Connect to BigQuery with Python
Encryption and decryption with Python