LDM-SyntheticChestX-Ray / create_video.py
lfolle's picture
Change to h264 mp4.
3fe56b6
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