
Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
2.7 KiB
Handling Animated Image Files
@tableofcontents
Original author | Suleyman Turkmen (with help of ChatGPT) |
Compatibility | OpenCV >= 4.11 |
Goal
In this tutorial, you will learn how to:
- Use
cv::imreadanimation
to load frames from animated image files. - Understand the structure and parameters of the
cv::Animation
structure. - Display individual frames from an animation.
- Use
cv::imwriteanimation
to writecv::Animation
to a file.
Source Code
@add_toggle_cpp
-
Downloadable code: Click here
-
Code at a glance: @include samples/cpp/tutorial_code/imgcodecs/animations.cpp @end_toggle
@add_toggle_python
-
Downloadable code: Click here
-
Code at a glance: @include samples/python/tutorial_code/imgcodecs/animations.py @end_toggle
Explanation
Initializing the Animation Structure
Initialize a cv::Animation
structure to hold the frames from the animated image file.
@add_toggle_cpp @snippet cpp/tutorial_code/imgcodecs/animations.cpp init_animation @end_toggle
@add_toggle_python @snippet python/tutorial_code/imgcodecs/animations.py init_animation @end_toggle
Loading Frames
Use cv::imreadanimation
to load frames from the specified file. Here, we load all frames from an animated WebP image.
@add_toggle_cpp @snippet cpp/tutorial_code/imgcodecs/animations.cpp read_animation @end_toggle
@add_toggle_python @snippet python/tutorial_code/imgcodecs/animations.py read_animation @end_toggle
Displaying Frames
Each frame in the animation.frames
vector can be displayed as a standalone image. This loop iterates through each frame, displaying it in a window with a short delay to simulate the animation.
@add_toggle_cpp @snippet cpp/tutorial_code/imgcodecs/animations.cpp show_animation @end_toggle
@add_toggle_python @snippet python/tutorial_code/imgcodecs/animations.py show_animation @end_toggle
Saving Animation
@add_toggle_cpp @snippet cpp/tutorial_code/imgcodecs/animations.cpp write_animation @end_toggle
@add_toggle_python @snippet python/tutorial_code/imgcodecs/animations.py write_animation @end_toggle
Summary
The cv::imreadanimation
and cv::imwriteanimation
functions make it easy to work with animated image files by loading frames into a cv::Animation
structure, allowing frame-by-frame processing.
With these functions, you can load, process, and save frames from animated image files like GIF, AVIF, APNG, and WebP.