Michele Mantovan

Creating a video barcode with Python

I’ve recently discovered this curious blog called movie barcode (link). The image on this blog are basically created from the frame colors of a movie or a video and maybe there are some post-processing on the image to make it more artistic. When I discovered it, I really liked the idea and I started to gather information on the possible algorithm to analyse and create (from code) similar images. This is when I discovered some articles (this one, and this one) on the matter and I started to play around with the code given. Most of it was in Python so I continued to use it for this project. My idea was to analyse video source frame by frame, then find the main color for that frame and to create an image that has a vertical line (of a certain width) of that color. I know this is a different approach from the ones on the blog , but I wanted to start easy and then to expand or change the algorithms. The link of the script is at the end of this article 🙂

Take a video source

The first step is to take a video source and for that you can choose to take a video on a specific path of your pc or you can download it. I used this library (link) to download a video from Youtube (very easy to use). Once we have a video we can analyse it. I tested my script mostly with mp4, avi and mkv videos, I don’t know if can works with all video codecs or compression. (it’s not the purpose of my script 🙂 )

Analyse frame by frame

Now the fun part: find the main color of a frame. To do that I massively inspired my work on this articles (link) from Alan Zucconi, where he uses a K-mean algorithm to analyse an image and explains very well how this algorithm works. Fortunately, there are some good Python libraries to do that (details on the script) and they are easy to use, so why not. This algorithm is pretty heavy but after few seconds we have the RGB components of the main color for that video frame and after doing this process for every frame of the video, we have a list of colors. Since I choosed to make the result images of a certain maximum size, in the script I analyse only a certain number of frames. If the number of frames are more than this number I simple analyse one frames and skip N frames.

Creating the final image

Now what we have to do is to create an image that have a vertical line for every color of our list of colors. To do this, I used another Python library to create images, graphs and color manipulation stuff, which is a pretty powerful library that can do various things as create an image of a certain size from a list (a matrix) of colors. Here is how the matrix is created.

Final results

I’m pretty happy with the results so far, so I started to analyse some Youtube videos and some films with this results:

It’s only a matter of install the right libraries and run this script with a video and you’ll have a nice barcode. I will definitely improve this code and try to make it more artistic. Maybe in the future there will be another post on this subject.

Source code