Skip to content

Instantly share code, notes, and snippets.

@pcgeek86
Last active February 11, 2024 22:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcgeek86/ae55fada48e6cf25516f2e064cfd0edb to your computer and use it in GitHub Desktop.
Save pcgeek86/ae55fada48e6cf25516f2e064cfd0edb to your computer and use it in GitHub Desktop.
Convert an animated .webp file to GIF with Python
#!/usr/bin/env python3
# Trevor Sullivan <trevor@trevorsullivan.net>
# https://trevorsullivan.net
# https://twitter.com/pcgeek86
# IMPORTANT: Install the webp Python package, using the following command:
# pip3 install --user webp
# Import the webp package
import webp
# Load a PIL image array from the specified .webp animation file
anim = webp.load_images('giphy.webp')
# Grab a reference to the first frame, and save the entire PIL image array as GIF with 70ms frames (14.286 FPS)
anim[0].save('output.gif', save_all=True, append_images=anim[0:], duration=70, loop=0)
@pcgeek86
Copy link
Author

pcgeek86 commented Sep 8, 2021

I can reproduce in Docker. Module must be broken.

docker run --rm -it --pull always python:latest bash
pip3 install webp
Collecting webp
  Using cached webp-0.1.1.tar.gz (10 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-kj0ifio4
         cwd: /tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/
    Complete output (71 lines):
    Configuration:
    [settings]
    arch=x86_64
    arch_build=x86_64
    build_type=Release
    compiler=gcc
    compiler.libcxx=libstdc++
    compiler.version=10
    os=Linux
    os_build=Linux
    [options]
    [build_requires]
    [env]

    libwebp/1.0.3@bincrafters/stable: Not found in local cache, looking in remotes...
    libwebp/1.0.3@bincrafters/stable: Trying with 'conancenter'...
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/setup.py", line 6, in <module>
        setup(
      File "/usr/local/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
        return distutils.core.setup(**attrs)
      File "/usr/local/lib/python3.9/distutils/core.py", line 108, in setup
        _setup_distribution = dist = klass(attrs)
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 455, in __init__
        _Distribution.__init__(self, {
      File "/usr/local/lib/python3.9/distutils/dist.py", line 292, in __init__
        self.finalize_options()
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 807, in finalize_options
        ep(self)
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 814, in _finalize_setup_keywords
        ep.load()(self, ep.name, value)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 219, in cffi_modules
        add_cffi_module(dist, cffi_module)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 49, in add_cffi_module
        execfile(build_file_name, mod_vars)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 25, in execfile
        exec(code, glob, glob)
      File "webp_build/builder.py", line 14, in <module>
        conan.install(path=getcwd(), cwd=tmp_dir)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/conan_api.py", line 93, in wrapper
        return f(api, *args, **kwargs)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/conan_api.py", line 606, in install
        deps_install(app=self.app,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/manager.py", line 57, in deps_install
        deps_graph = graph_manager.load_graph(ref_or_path, create_reference, graph_info, build_modes,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 120, in load_graph
        deps_graph = self._resolve_graph(root_node, profile_host, profile_build, graph_lock,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 276, in _resolve_graph
        deps_graph = self._load_graph(root_node, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 384, in _load_graph
        graph = builder.load_graph(root_node, check_updates, update, remotes, profile_host,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 66, in load_graph
        self._expand_node(root_node, dep_graph, Requirements(), None, None, check_updates,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 131, in _expand_node
        self._expand_require(require, node, graph, check_updates, update, remotes, profile_host,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 246, in _expand_require
        new_node = self._create_new_node(node, graph, require, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 441, in _create_new_node
        result = self._resolve_recipe(current_node, dep_graph, requirement, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 407, in _resolve_recipe
        raise e
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 401, in _resolve_recipe
        result = self._proxy.get_recipe(requirement.ref, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 37, in get_recipe
        result = self._get_recipe(layout, ref, check_updates, update, remotes, recorder)
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 53, in _get_recipe
        remote, new_ref = self._download_recipe(layout, ref, output, remotes, remotes.selected,
      File "/tmp/pip-install-a2pc3fn4/webp_045f07ce200a4b5590d43118330d75c9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 173, in _download_recipe
        raise NotFoundException(msg)
    conans.errors.NotFoundException: Unable to find 'libwebp/1.0.3@bincrafters/stable' in remotes
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/58/90/44ac4929b2f293a58741876a177bf62f334c45bef4d0adc43b713203a1c1/webp-0.1.1.tar.gz#sha256=f4391dd6f56c1eef9df54071240ce0f3cf9bfb64320a4c8cf97d8a40bfd0eac9 (from https://pypi.org/simple/webp/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  Using cached webp-0.1.0.tar.gz (10 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-5ylcqaq0
         cwd: /tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/
    Complete output (71 lines):
    Configuration:
    [settings]
    arch=x86_64
    arch_build=x86_64
    build_type=Release
    compiler=gcc
    compiler.libcxx=libstdc++
    compiler.version=10
    os=Linux
    os_build=Linux
    [options]
    [build_requires]
    [env]

    libwebp/1.0.3@bincrafters/stable: Not found in local cache, looking in remotes...
    libwebp/1.0.3@bincrafters/stable: Trying with 'conancenter'...
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/setup.py", line 6, in <module>
        setup(
      File "/usr/local/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
        return distutils.core.setup(**attrs)
      File "/usr/local/lib/python3.9/distutils/core.py", line 108, in setup
        _setup_distribution = dist = klass(attrs)
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 455, in __init__
        _Distribution.__init__(self, {
      File "/usr/local/lib/python3.9/distutils/dist.py", line 292, in __init__
        self.finalize_options()
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 807, in finalize_options
        ep(self)
      File "/usr/local/lib/python3.9/site-packages/setuptools/dist.py", line 814, in _finalize_setup_keywords
        ep.load()(self, ep.name, value)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 219, in cffi_modules
        add_cffi_module(dist, cffi_module)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 49, in add_cffi_module
        execfile(build_file_name, mod_vars)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/cffi-1.14.6-py3.9-linux-x86_64.egg/cffi/setuptools_ext.py", line 25, in execfile
        exec(code, glob, glob)
      File "webp_build/builder.py", line 14, in <module>
        conan.install(path=getcwd(), cwd=tmp_dir)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/conan_api.py", line 93, in wrapper
        return f(api, *args, **kwargs)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/conan_api.py", line 606, in install
        deps_install(app=self.app,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/manager.py", line 57, in deps_install
        deps_graph = graph_manager.load_graph(ref_or_path, create_reference, graph_info, build_modes,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 120, in load_graph
        deps_graph = self._resolve_graph(root_node, profile_host, profile_build, graph_lock,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 276, in _resolve_graph
        deps_graph = self._load_graph(root_node, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_manager.py", line 384, in _load_graph
        graph = builder.load_graph(root_node, check_updates, update, remotes, profile_host,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 66, in load_graph
        self._expand_node(root_node, dep_graph, Requirements(), None, None, check_updates,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 131, in _expand_node
        self._expand_require(require, node, graph, check_updates, update, remotes, profile_host,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 246, in _expand_require
        new_node = self._create_new_node(node, graph, require, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 441, in _create_new_node
        result = self._resolve_recipe(current_node, dep_graph, requirement, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 407, in _resolve_recipe
        raise e
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/graph_builder.py", line 401, in _resolve_recipe
        result = self._proxy.get_recipe(requirement.ref, check_updates, update,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 37, in get_recipe
        result = self._get_recipe(layout, ref, check_updates, update, remotes, recorder)
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 53, in _get_recipe
        remote, new_ref = self._download_recipe(layout, ref, output, remotes, remotes.selected,
      File "/tmp/pip-install-a2pc3fn4/webp_f5dc338add9143ad8f6a8b5a8e48abf9/.eggs/conan-1.40.0-py3.9.egg/conans/client/graph/proxy.py", line 173, in _download_recipe
        raise NotFoundException(msg)
    conans.errors.NotFoundException: Unable to find 'libwebp/1.0.3@bincrafters/stable' in remotes
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/62/df/1efb36189ee596f6d3773c8c1b051ada90d244a601b0e4de00181e8fa6f9/webp-0.1.0.tar.gz#sha256=9f15bf22999789b05f12dd02e0970d44adf506e086ea9558e6c5abc4c5585126 (from https://pypi.org/simple/webp/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement webp (from versions: 0.1.0a4, 0.1.0a5, 0.1.0a6, 0.1.0a7, 0.1.0a9, 0.1.0a10, 0.1.0a11, 0.1.0a12, 0.1.0a13, 0.1.0a14, 0.1.0a15, 0.1.0a16, 0.1.0, 0.1.1)
ERROR: No matching distribution found for webp

@FeraruSilviuMarian
Copy link

FeraruSilviuMarian commented Sep 8, 2021

I noticed my python is the 32 bit version, and the webptools executables are 64 bit, maybe that's why I can't install neither webp or webptools modules. I'll try to test this hypothesis with a virtual machine so I don't mess up something

@FeraruSilviuMarian
Copy link

FeraruSilviuMarian commented Sep 13, 2021

The two methods (webp and PIL) yield the same poor image quality, the colors are fewer, the colors feel 8 bit or so

`import webp
from PIL import Image

im = Image.open(r'C:\Users\silve\Downloads\25036996.webp')
im.info.pop('background', None)
im.save(r'C:\Users\silve\Downloads\test.gif', 'gif', save_all=True)

anim = webp.load_images(r'C:\Users\silve\Downloads\25036996.webp')
anim[0].save(r'C:\Users\silve\Downloads\test2.gif', save_all=True, append_images=anim[0:], duration=70, loop=0)`

@Mostalk
Copy link

Mostalk commented May 12, 2022

Hi, ty for this script. But, after conver my gif is very speed up? How to increase speed?

@tzelalouzeir
Copy link

i

from PIL import Image

import requests
from io import BytesIO

#webp to gif convert
#webp link
url = 'https://cdn.7tv.app/emote/60a9cfe96daf811370b0b640/4x.webp'
#save image destination
destination = 'output1.gif'

response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.save(destination,'gif',save_all=True)

founded from: https://groups.google.com/a/webmproject.org/g/webp-discuss/c/VcfpaZ0fJeM?pli=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment