Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GIF palette optimization #104

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add GIF palette optimization
rkfg committed Sep 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 655fb61017e0f0b8ae11bab85e00b00777787804
9 changes: 9 additions & 0 deletions scripts/animatediff.py
Original file line number Diff line number Diff line change
@@ -63,6 +63,15 @@ def on_ui_settings():
section=section,
),
)
shared.opts.add_option(
"animatediff_optimize_gif_palette",
shared.OptionInfo(
False,
"Calculate the optimal GIF palette, improves quality significantly, removes banding",
gr.Checkbox,
section=section
)
)


script_callbacks.on_ui_settings(on_ui_settings)
50 changes: 41 additions & 9 deletions scripts/animatediff_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import imageio
import imageio.v3 as imageio
from pathlib import Path
import numpy as np

from modules import images
from modules import images, shared
from modules.processing import StableDiffusionProcessing, Processed

from scripts.animatediff_logger import logger_animatediff as logger
@@ -48,21 +49,52 @@ def _save(
index: int,
):
video_paths = []
video_array = [np.array(v) for v in video_list]
if "GIF" in params.format:
video_path_gif = video_path_prefix + "gif"
video_paths.append(video_path_gif)
imageio.mimsave(
video_path_gif,
video_list,
duration=(1 / params.fps),
loop=params.loop_number,
)
if shared.opts.data.get("animatediff_optimize_gif_palette", True):
try:
import av
except ImportError:
from launch import run_pip
run_pip(
"install imageio[pyav]",
"sd-webui-animatediff GIF palette optimization requirement: imageio[pyav]",
)
imageio.imwrite(
video_path_gif, video_array, plugin='pyav', fps=params.fps,
codec='gif', out_pixel_format='pal8',
filter_graph=(
{
"split": ("split", ""),
"palgen": ("palettegen", ""),
"paluse": ("paletteuse", ""),
"scale": ("scale", f"{video_list[0].width}:{video_list[0].height}")
},
[
("video_in", "scale", 0, 0),
("scale", "split", 0, 0),
("split", "palgen", 1, 0),
("split", "paluse", 0, 0),
("palgen", "paluse", 0, 1),
("paluse", "video_out", 0, 0),
]
)
)
else:
imageio.imwrite(
video_path_gif,
video_array,
duration=(1000 / params.fps),
loop=params.loop_number,
)
if "Optimize GIF" in params.format:
self._optimize_gif(video_path_gif)
if "MP4" in params.format:
video_path_mp4 = video_path_prefix + "mp4"
video_paths.append(video_path_mp4)
imageio.mimsave(video_path_mp4, video_list, fps=params.fps)
imageio.imwrite(video_path_mp4, video_array, fps=params.fps, codec="h264")
if "TXT" in params.format and res.images[index].info is not None:
video_path_txt = video_path_prefix + "txt"
self._save_txt(params, video_path_txt, res, index)