From video to thumbnails with Python and OpenCV
In this article I want to explain you how you can create thumbnails starting from a pre-recorded video using Python and OpenCV library. You need Python 2.7+ and OpenCV 3 installed on your machine before starting. For Mac OS X you can follow this tutorial, while for Ubuntu you can follow this one.
From video to frames
The following function takes the video filename as input and it returns an array of OpenCV images containing first frame, frames at 1/4, 2/4, 3/4 of the video’s length and the last frame.
From frame to thumbs
We also need something to create the thumbnails for each extracted frame. The following function takes as input an OpenCV image (frame) and it returns a dictionary of images (thumbnails plus original). Generated thumbs have constant width of 640 px, 320 px and 160 px width. Please note the function uses INTER_AREA as interpolation method when resizing (it resamples using pixel area relation and it is generally the preferred method for image decimation).
All together
Let’s now see how we can use these two functions together to extract the frames from the video and create thumbnails for one of each.
The code above first call video_to_frames to extract the frames from the video, then it calls image_to_thumbs to create the thumbnails for each frame saving these locally on the disk using the OpenCV function imwrite.