| | | | -: | :- | | Original author | Suleyman Turkmen (with help of ChatGPT) | | Compatibility | OpenCV >= 4.11 |
In this tutorial, you will learn how to:
cv::imreadanimation to load frames from animated image files.cv::Animation structure.cv::imwriteanimation to write cv::Animation to a file.Downloadable code: Click here
Code at a glance: @include samples/cpp/tutorial_code/imgcodecs/animations.cpp @end_toggle
Downloadable code: Click here
Code at a glance: @include samples/python/tutorial_code/imgcodecs/animations.py @end_toggle
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
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
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.
Note: Frame durations in
cv::Animationare expressed in milliseconds. When displaying frames manually usingcv::waitKey, make sure to use the corresponding duration value to preserve the original animation timing.
@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
@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
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.