Created
March 6, 2018 03:46
-
-
Save anhtran/8ad072ed8edf44a4b7eb80e5a1a63c04 to your computer and use it in GitHub Desktop.
Example python code for saving animated GIF into file using Pillow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (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