File: barcode.py

package info (click to toggle)
python-av 16.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,684 kB
  • sloc: python: 7,607; sh: 182; ansic: 174; makefile: 135
file content (34 lines) | stat: -rw-r--r-- 871 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)

# Close the file, free memory
container.close()

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)