Skip to content

Commit

Permalink
Add WebP output with infotext stored in exif and options for quality …
Browse files Browse the repository at this point in the history
…and lossless (#233)

Co-authored-by: zappityzap <you@example.com>
  • Loading branch information
zappityzap and zappityzap committed Oct 20, 2023
1 parent 565fd34 commit 15dcc49
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
22 changes: 22 additions & 0 deletions scripts/animatediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ def on_ui_settings():
section=section
)
)
shared.opts.add_option(
"animatediff_webp_quality",
shared.OptionInfo(
80,
"WebP Quality (if lossless=True, increases compression and CPU usage)",
gr.Slider,
{
"minimum": 1,
"maximum": 100,
"step": 1},
section=section
)
)
shared.opts.add_option(
"animatediff_webp_lossless",
shared.OptionInfo(
False,
"Save WebP in lossless format (highest quality, largest file size)",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
"animatediff_save_to_custom",
shared.OptionInfo(
Expand Down
21 changes: 21 additions & 0 deletions scripts/animatediff_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import imageio.v3 as imageio
import numpy as np
from PIL import Image, PngImagePlugin
import PIL.features
import piexif
from modules import images, shared
from modules.processing import Processed, StableDiffusionProcessing

Expand Down Expand Up @@ -219,6 +221,25 @@ def _save(
if "TXT" in params.format and res.images[index].info is not None:
video_path_txt = video_path_prefix + ".txt"
self._save_txt(video_path_txt, infotext)
if "WEBP" in params.format:
if PIL.features.check('webp_anim'):
video_path_webp = video_path_prefix + ".webp"
video_paths.append(video_path_webp)
if use_infotext:
exif_bytes = piexif.dump({
"Exif":{
piexif.ExifIFD.UserComment:piexif.helper.UserComment.dump(infotext, encoding="unicode")
}})
lossless = shared.opts.data.get("animatediff_webp_lossless", False)
quality = shared.opts.data.get("animatediff_webp_quality", 80)
logger.info(f"Saving {video_path_webp} with lossless={lossless} and quality={quality}")
imageio.imwrite(video_path_webp, video_array, plugin='pillow',
duration=int(1 / params.fps * 1000), loop=params.loop_number,
lossless=lossless, quality=quality, exif=exif_bytes
)
# see additional Pillow WebP options at https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp
else:
logger.warn("WebP animation in Pillow requires system WebP library v0.5.0 or later")
return video_paths

def _optimize_gif(self, video_path: str):
Expand Down
4 changes: 2 additions & 2 deletions scripts/animatediff_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _check(self):
assert (
self.video_length >= 0 and self.fps > 0
), "Video length and FPS should be positive."
assert not set(["GIF", "MP4", "PNG"]).isdisjoint(
assert not set(["GIF", "MP4", "PNG", "WEBP"]).isdisjoint(
self.format
), "At least one saving format should be selected."

Expand Down Expand Up @@ -207,7 +207,7 @@ def refresh_models(*inputs):
)
with gr.Row():
self.params.format = gr.CheckboxGroup(
choices=["GIF", "MP4", "PNG", "TXT"],
choices=["GIF", "MP4", "PNG", "TXT", "WEBP"],
label="Save format",
type="value",
elem_id=f"{elemid_prefix}save-format",
Expand Down

0 comments on commit 15dcc49

Please sign in to comment.