File size: 1,481 Bytes
e5a19d6
 
dcf2dd9
e5a19d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fe56b6
 
e5a19d6
 
 
 
 
 
 
 
 
 
dcf2dd9
460256d
dcf2dd9
3fe56b6
 
 
e5a19d6
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
35
36
37
38
import numpy as np
from PIL import Image
import ffmpeg


def noise_process(numpy_image, steps=149):
    noisy_image_list = []
    noisy_image = numpy_image
    noisy_image_list.append(noisy_image)

    for step in range(steps):
        noise = 255 * np.random.normal(0, 0.07*(step+1) * 0.1, numpy_image.size).reshape(numpy_image.shape)
        noisy_image = noisy_image + noise
        noisy_image_list.append(noisy_image)
    return noisy_image_list


def generate_video(numpy_image):
    # save_path = "result.ogg"
    save_path = "result.mp4"
    fps = 30
    sec = 5
    image_lst = noise_process(numpy_image)
    image_lst = np.array([(i-np.min(i))/(np.max(i)-np.min(i)) for i in image_lst])
    image_lst = np.round(image_lst * 255).astype(np.uint8)
    copies = int((sec * fps) / len(image_lst))
    spill_over = sec * fps - copies * len(image_lst)
    image_lst = np.repeat(image_lst, copies, axis=0)
    image_lst = np.concatenate((image_lst, image_lst[:spill_over]), axis=0)
    image_lst = image_lst[::-1]
    for i, img in enumerate(image_lst):
        Image.fromarray(img).save(f"video/{i:03d}.jpg", quality=95)

    # ffmpeg.input('video/*.jpg', pattern_type='glob').output(save_path, qscale=10).run(overwrite_output=True)
    # ffmpeg.input('video/%3d.jpg').output(save_path, crf=5, vcodec="h264").run(overwrite_output=True)
    ffmpeg.input('video/*.jpg', pattern_type='glob').output(save_path, crf=5, vcodec="h264").run(overwrite_output=True)
    return save_path