Skip to content

Instantly share code, notes, and snippets.

@anhtran
Created March 6, 2018 03:46
Show Gist options
  • Save anhtran/8ad072ed8edf44a4b7eb80e5a1a63c04 to your computer and use it in GitHub Desktop.
Save anhtran/8ad072ed8edf44a4b7eb80e5a1a63c04 to your computer and use it in GitHub Desktop.
Example python code for saving animated GIF into file using Pillow
# (c) 2018 Anh Tran <anhtran.net>
import requests
from PIL import ImageSequence
def fetch_chap_img(img_url):
""" Get image from internet then saving it on disk """
r = requests.head(img_url)
if r.status_code == 200:
r = requests.get(img_url)
if r.ok:
o = Image.open(BytesIO(r.content))
if o.format == 'GIF':
# get frames
frames = [frame.copy() for frame in ImageSequence.Iterator(o)]
# write GIF animation
fn = '{}.{}'.format('gif_file_name', 'gif')
o.save(fn, save_all=True, append_images=frames, quality=75, **o.info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment