Pillow – cannot save resized/cropped gif
Question:
I am using the Pillow
package to change some gifs to animated .webp’s with a square size.
The issue is that the saved file isn’t resized/cropped.
This is my code:
destination = Path(source).with_suffix(".webp")
extension = ".webp"
im = Image.open(source)
width, height = im.size
if width != height:
if width > height:
left = (width - height)/2
top = 0
right = (width + height)/2
bottom = height
else:
left = 0
top = (height - width)/2
right = width
bottom = (height + width)/2
if getattr(im, "is_animated", False):
frames = ImageSequence.Iterator(im)
cropped_frames = []
for frame in frames:
cropped_frames.append(frame.crop((left, top, right, bottom)))
im.save(destination, format="webp", save_all=True, optimize=True, append_images=list(frames), loop=0)
Answers:
I believe you want to do this?
save cropped_frames
not frames
im.save(destination, format="webp", save_all=True, optimize=True, append_images=cropped_frames, loop=0)
I am using the Pillow
package to change some gifs to animated .webp’s with a square size.
The issue is that the saved file isn’t resized/cropped.
This is my code:
destination = Path(source).with_suffix(".webp")
extension = ".webp"
im = Image.open(source)
width, height = im.size
if width != height:
if width > height:
left = (width - height)/2
top = 0
right = (width + height)/2
bottom = height
else:
left = 0
top = (height - width)/2
right = width
bottom = (height + width)/2
if getattr(im, "is_animated", False):
frames = ImageSequence.Iterator(im)
cropped_frames = []
for frame in frames:
cropped_frames.append(frame.crop((left, top, right, bottom)))
im.save(destination, format="webp", save_all=True, optimize=True, append_images=list(frames), loop=0)
I believe you want to do this?
save cropped_frames
not frames
im.save(destination, format="webp", save_all=True, optimize=True, append_images=cropped_frames, loop=0)