Debian Codegraph @

examples/numpy/barcode.py

raw
1 import numpy as np
2 from PIL import Image
3
4 import av
5 import av.datasets
6
7 container = av.open(
8 av.datasets.curated("pexels/time-lapse-video-of-sunset-by-the-sea-854400.mp4")
9 )
10 container.streams.video[0].thread_type = "AUTO" # Go faster!
11
12 columns = []
13 for frame in container.decode(video=0):
14 print(frame)
15 array = frame.to_ndarray(format="rgb24")
16
17 # Collapse down to a column.
18 column = array.mean(axis=1)
19
20 # Convert to bytes, as the `mean` turned our array into floats.
21 column = column.clip(0, 255).astype("uint8")
22
23 # Get us in the right shape for the `hstack` below.
24 column = column.reshape(-1, 1, 3)
25
26 columns.append(column)
27
28 # Close the file, free memory
29 container.close()
30
31 full_array = np.hstack(columns)
32 full_img = Image.fromarray(full_array, "RGB")
33 full_img = full_img.resize((800, 200))
34 full_img.save("barcode.jpg", quality=85)