Making Video Into Barcode
It’s a video, in a single graphic.
This project was inspired by a chance encounter on Reddit about “movie barcodes” and thought that it was pretty genius — a movie represented in what’s essentially a series of colored bars (more examples can be found here). I wanted to try making graphical barcodes on a smaller scale, of videos in a subsequent thought.
Making Barcodes
There are 3 steps in the process:
- Getting each frame from the video
- “Shrinking” each frame into bar
- Combining the bars to make the barcode
Preliminary Notes:
- Code examples used in this writing can be found here
- The video used in the examples is not on GitHub, you would have to find your own video file
- For the sake of this example, I have a video file saved locally as “video.mp4” and the source is here
Step 1: Getting each frame from the video
This step uses cv2 to go through the video and capture each second as an image, or “frame” in a folder.
Step 2: “Shrinking” each frame into bar
That just refers to reducing (resizing) each frame’s width into a size that resembles a bar (narrow width, tall height). Interpolation is set to INTER_AREA, which according to cv2’s documentation is preferred for “image decimation”, or shrinking.
# reducing the size of image frame to a bar (1 x 920)bar_width = 1
barcode_height = 920bar = cv2.resize(frame, (bar_width, barcode_height), interpolation = cv2.INTER_AREA)
Step 3: Combining the bars to make the barcode
After getting the bars, what’s left is to put them together. In the code snippet below, each frame (the result of step 1) is read, reduced to a bar, and added onto a blank image to make up a barcode in a loop.
Combining all the snippets:
The resulting barcode:
It didn’t come out too bad.
Ideas for Further Development
The code is but a base version, other more interesting things can be built on it, such as:
- Using the barcode as a background (e.g. for typography)
- Using more or less frequent frame extraction intervals (e.g. getting each frame half a second / 5 second apart)
- Changing how the bars look or lay (e.g. horizontally, at an angle, using circles, arcs)