forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimations.py
More file actions
52 lines (43 loc) · 1.61 KB
/
animations.py
File metadata and controls
52 lines (43 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cv2 as cv
import numpy as np
def main(filename):
## [write_animation]
if filename == "animated_image.webp":
# Create an Animation instance to save
animation_to_save = cv.Animation()
# Generate a base image with a specific color
image = np.full((128, 256, 4), (150, 150, 150, 255), dtype=np.uint8)
duration = 200
frames = []
durations = []
# Populate frames and durations in the Animation object
for i in range(10):
frame = image.copy()
cv.putText(frame, f"Frame {i}", (30, 80), cv.FONT_HERSHEY_SIMPLEX, 1.5, (255, 100, 0, 255), 2)
frames.append(frame)
durations.append(duration)
animation_to_save.frames = frames
animation_to_save.durations = durations
# Write the animation to file
cv.imwriteanimation(filename, animation_to_save, [cv.IMWRITE_WEBP_QUALITY, 100])
## [write_animation]
## [init_animation]
animation = cv.Animation()
## [init_animation]
## [read_animation]
success, animation = cv.imreadanimation(filename)
if not success:
print("Failed to load animation frames")
return
## [read_animation]
## [show_animation]
while True:
for i, frame in enumerate(animation.frames):
cv.imshow("Animation", frame)
key_code = cv.waitKey(animation.durations[i])
if key_code == 27: # Exit if 'Esc' key is pressed
return
## [show_animation]
if __name__ == "__main__":
import sys
main(sys.argv[1] if len(sys.argv) > 1 else "animated_image.webp")