Numpy¶
Video Barcode¶
A video barcode shows the change in colour and tone over time. Time is represented on the horizontal axis, while the vertical remains the vertical direction in the image.
See http://moviebarcode.tumblr.com/ for examples from Hollywood movies, and here is an example from a sunset timelapse:
The code that created this:
import numpy as np
from PIL import Image
import av
import av.datasets
container = av.open(av.datasets.curated('pexels/time-lapse-video-of-sunset-by-the-sea-854400.mp4'))
container.streams.video[0].thread_type = 'AUTO' # Go faster!
columns = []
for frame in container.decode(video=0):
print(frame)
array = frame.to_ndarray(format='rgb24')
# Collapse down to a column.
column = array.mean(axis=1)
# Convert to bytes, as the `mean` turned our array into floats.
column = column.clip(0, 255).astype('uint8')
# Get us in the right shape for the `hstack` below.
column = column.reshape(-1, 1, 3)
columns.append(column)
full_array = np.hstack(columns)
full_img = Image.fromarray(full_array, 'RGB')
full_img = full_img.resize((800, 200))
full_img.save('barcode.jpg', quality=85)
Generating Video¶
from __future__ import division
import numpy as np
import av
duration = 4
fps = 24
total_frames = duration * fps
container = av.open('test.mp4', mode='w')
stream = container.add_stream('mpeg4', rate=fps)
stream.width = 480
stream.height = 320
stream.pix_fmt = 'yuv420p'
for frame_i in range(total_frames):
img = np.empty((480, 320, 3))
img[:, :, 0] = 0.5 + 0.5 * np.sin(2 * np.pi * (0 / 3 + frame_i / total_frames))
img[:, :, 1] = 0.5 + 0.5 * np.sin(2 * np.pi * (1 / 3 + frame_i / total_frames))
img[:, :, 2] = 0.5 + 0.5 * np.sin(2 * np.pi * (2 / 3 + frame_i / total_frames))
img = np.round(255 * img).astype(np.uint8)
img = np.clip(img, 0, 255)
frame = av.VideoFrame.from_ndarray(img, format='rgb24')
for packet in stream.encode(frame):
container.mux(packet)
# Flush stream
for packet in stream.encode():
container.mux(packet)
# Close the file
container.close()