Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
deforum authored Oct 4, 2023
1 parent 34fad46 commit 8e05cf5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion hotshot_xl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from PIL import ImageSequence, Image
import requests
import os
import numpy as np
import imageio


def get_image(img_path) -> PIL.Image.Image:
if img_path.startswith("http"):
Expand All @@ -41,11 +44,23 @@ def images_to_gif_bytes(images: List, duration: int = 1000) -> bytes:

return gif_bytes


def save_as_gif(images: List, file_path: str, duration: int = 1000):
with open(file_path, "wb") as f:
f.write(images_to_gif_bytes(images, duration))

def images_to_mp4_bytes(images: List[Image.Image], duration: int = 1000) -> bytes:
with BytesIO() as output_buffer:
with imageio.get_writer(output_buffer, format='mp4', fps=1/(duration/1000)) as writer:
for img in images:
writer.append_data(np.array(img))
mp4_bytes = output_buffer.getvalue()

return mp4_bytes

def save_as_mp4(images: List[Image.Image], file_path: str, duration: int = 1000):
with open(file_path, "wb") as f:
f.write(images_to_mp4_bytes(images, duration))

def scale_aspect_fill(img, new_width, new_height):
new_width = int(new_width)
new_height = int(new_height)
Expand Down

0 comments on commit 8e05cf5

Please sign in to comment.