66from .utils import (
77 get_animation_duration_in_seconds ,
88 get_frame_lengths ,
9- get_animation_frames ,
9+ extract_animation_frames ,
1010)
1111
1212import io
@@ -20,31 +20,39 @@ def encode_as_webp(
2020 if not PILFeatures .check ("webp_anim" ):
2121 raise RuntimeError ("WebP animation support not available, WeBP library outdated?" )
2222
23+ frames = extract_animation_frames (pillow )
24+
2325 if config .resize is not None :
2426 im_size = (pillow .width , pillow .height )
2527 size = calculate_downscale (im_size , config .resize )
26- pillow = pillow .resize (size , PILImage .LANCZOS )
28+ frames = [
29+ frame .resize (size , PILImage .LANCZOS )
30+ for frame in frames
31+ ]
2732
33+ first_frame = frames [0 ]
2834 buf = io .BytesIO ()
29- pillow .save (
35+ first_frame .save (
3036 fp = buf ,
3137 format = 'webp' ,
3238 lossless = config .lossless ,
3339 quality = config .quality ,
40+ allow_mixed = not config .lossless , # Allow mixed lossy/lossless frames
41+ minimize_size = True ,
42+
3443 save_all = True , # Save as an animation
44+ append_images = frames [1 :],
3545 transparency = 0 ,
3646 duration = get_frame_lengths (pillow ),
3747 background = (0 , 0 , 0 , 0 ), # Transparent background
38- minimize_size = True ,
39- allow_mixed = not config .lossless ,
4048 disposal = 2 ,
4149 )
4250
4351 return AnimationFile (
4452 data = buf .getvalue (),
4553 mimetype = 'image/webp' ,
46- width = pillow .width ,
47- height = pillow .height ,
54+ width = first_frame .width ,
55+ height = first_frame .height ,
4856 frame_count = pillow .n_frames ,
4957 duration = get_animation_duration_in_seconds (pillow ),
5058 )
0 commit comments