Skip to content

Support WebP Animations #604

Closed
Closed

Description

@Skylion007

WebP can support animations too (ie multiple frames). FFMPEG can generate these, but I am unsure how to add this PR since WebP is already supported for single images. What would be the best way to add this functionality? Would just adding webp to the support ffmpeg extensions solve the issue?

Activity

  1. FirefoxMetzger commented on Feb 27, 2021

    @FirefoxMetzger
    Contributor

    It's actually already possible to write webp in imageio if you manually select the plugin. Here is my tinker in the new API (#574 ):

    import imageio as iio
    im = iio.new_api.imread("random.gif", legacy_plugin=False, mode="RGB")
    
    # FFMPEG isn't ported yet
    with iio.imopen("test.webp", legacy_plugin=True, plugin="FFMPEG") as file:
        writer = file.legacy_get_writer(iio_mode="I", codec="libwebp", fps=1)
        for frame in im:
            writer.append_data(frame)
        writer.close()

    which translates to the current (v2.9) API as something like

    import imageio as iio
    frames = iio.mimread("random.gif", pilmode="RGB")  # Note: frames is a list not an array
    writer = iio.get_writer("test.webp", format="FFMPEG", mode="I", codec="libwebp", fps=25)
    for frame in im:
        writer.append_data(frame)
    writer.close()
    

    This works, because specifying a format= skips the search for a compatible plugin and instead assumes that the user-specified plugin can handle the format. If you want to add "native" support for webp ala iio.get_writer("test.webp"), you will have to do two things:

    1. Add webm to the list of supported formats (as you suggested), so that ffmpeg will be seen as a compatible plugin during the search.
    2. Add logic to FFmpegFormat to switch to the libwebp codec when required. For writing, checking the file extension will be sufficient.

    I didn't investigate reading. @almarklein probably knows the correct options here, and can also tell you about the procedure for implementing mimread.

  2. almarklein commented on Mar 1, 2021

    @almarklein
    Member

    Shouldn't this work?

    iio.mimwrite("test.webp", format="FFMPEG", codec="libwebp", fps=25)
  3. FirefoxMetzger commented on Mar 2, 2021

    @FirefoxMetzger
    Contributor

    Good catch. You can definitely use mimwrite as a one-liner for writing. I guess it should then have the image as second argument, i.e.,

    iio.mimwrite("test.webp", frames, format="FFMPEG", codec="libwebp", fps=25)

    My question was more about reading, because codec is currently not a supported argument for mimread with imageio-ffmpeg. Not sure which changes would need to be made to support it. What would be even better would be to recognize webp and configure the reader behind the scenes.

  4. FirefoxMetzger commented on Nov 7, 2021

    @FirefoxMetzger
    Contributor

    @Skylion007 Do you happen to know where I can find an example WEBP image?

    We can already read/write WEBP via FreeImage, and pillow should support this as well (in which case I can add a fast-path for it). The FFMPEG route would likely still require a bit more work; mainly because I only have surface-level knowledge about imageio-ffmpeg. I would like to test this first though before proposing anything "official".

  5. FirefoxMetzger commented on Mar 5, 2022

    @FirefoxMetzger
    Contributor

    recent versions of pillow support WEBP, and - as such - we support it, too :) I've added a fast path and documentation for this in #752 so I will close this issue once it that PR is merged.

    Regarding FFMPEGs ability to read/write webp, this will be added by #745 and I will add documentation for this in a future PR.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions