show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次制作的时钟
- 数字式
- 表盘式
- 在此过程中使用PIL包
- 使用系统字体 在矩阵中进行了绘画
- 而且让画面动了起来
- 可以把动画保存下来吗?🤔
- 首先要安装一个读写文件的工具
- imageio
pip install imageio
- 安装之后查看帮助
firefox https://pypi.org/project/imageio/
- 例程显示读写方法
>>> import imageio
>>> im = imageio.imread('imageio:astronaut.png')
>>> im.shape # im is a numpy array
(512, 512, 3)
>>> imageio.imwrite('astronaut-gray.jpg', im[:, :, 0])
- 那如何将动画保存成gif格式呢?
- 通过查询可以使用imageio完成
firefox https://github.com/imageio/imageio
import numpy as np
import imageio
import cv2
from random import randint
black = np.zeros((480,640,3), np.uint8)
white = np.zeros((480,640,3), np.uint8)
white[:] = (255,255,255)
image_lst = []
image_lst.append(black)
image_lst.append(white)
imageio.mimsave('./test.gif', image_lst, fps=1)
- 一秒黑,然后一直都是白的
firefox test.gif
- 可以做彩色闪动的效果吗?
import numpy as np
import imageio
import cv2
from random import randint
red = np.zeros((480,640,3), np.uint8)
green = np.zeros((480,640,3), np.uint8)
blue = np.zeros((480,640,3), np.uint8)
blue[:] = (0,0,255)
green[:] = (0,255,0)
red[:] = (255,0,0)
image_lst = [red,green,blue]
imageio.mimsave('./test.gif', image_lst, fps=1)
- 刷新网页
- 红绿蓝各一秒
- 然后停止
- 可以无限循环吗?
imageio.mimsave('./test.gif', image_lst,duration=0.5,loop=0)
- 修改最后一句
- fps 帧频率
- duration 每帧持续时间
- loop 循环次数 0为无限
- 刷新网页观察
- 期待效果
import cv2
import imageio
import numpy as np
from random import randint
before = cv2.imread("/home/shiyanlou/tiktok.jpg")
image_list = []
for i in range(30):
after = np.zeros((300,300,3),np.uint8)
r = randint(0,50)
print(r)
after[r:300,r:300,0] = before[0:300-r,0:300-r,0]
after[0:300-r,0:300-r,1] = before[r:300,r:300,1]
after[0:300-r,0:300-r,2] = before[r:300,r:300,2]
image_list.append(after)
imageio.mimsave("./test.gif",image_list,fps=30)
- 问题是方向太过固定
- 能否更加随机呢?
import cv2
import imageio
import numpy as np
from random import randint
before = cv2.imread("/home/shiyanlou/tiktok.jpg")
image_list = []
for i in range(30):
after = np.zeros((300,300,3),np.uint8)
r0 = randint(0,50) - 25
r1 = randint(0,50) - 25
r2 = randint(0,50) - 25
r3 = randint(0,50) - 25
if r0 >= 0 and r1 >= 0:
after[r0:300,r1:300,0] = before[0:300-r0,0:300-r1,0]
elif r0 >=0 and r1<0:
after[r0:300,0:300+r1,0] = before[0:300-r0,-r1:300,0]
elif r0<0 and r1>=0:
after[0:300+r0,r1:300,0] = before[-r0:300,0:300-r1,0]
else:
after[0:300+r0,0:300+r1,0] = before[-r0:300,-r1:300,0]
if r2 >= 0 and r3 >= 0:
after[r2:300,r3:300,1:3] = before[0:300-r2,0:300-r3,1:3]
elif r2 >=0 and r3<0:
after[r2:300,0:300+r3,1:3] = before[0:300-r2,-r3:300,1:3]
elif r2<0 and r3>=0:
after[0:300+r2,r3:300,1:3] = before[-r2:300,0:300-r3,1:3]
else:
after[0:300+r2,0:300+r3,1:3] = before[-r2:300,-r3:300,1:3]
image_list.append(after)
imageio.mimsave("./test.gif",image_list,fps=30)
- 运行效果
- 可以慢下来 并且看清每次位移吗?
import cv2
import imageio
import numpy as np
from random import randint
before = cv2.imread("/home/shiyanlou/tiktok.jpg")
image_list = []
for i in range(30):
after = np.zeros((300,300,3),np.uint8)
r0 = randint(0,50) - 25
r1 = randint(0,50) - 25
r2 = randint(0,50) - 25
r3 = randint(0,50) - 25
if r0 >= 0 and r1 >= 0:
after[r0:300,r1:300,0] = before[0:300-r0,0:300-r1,0]
elif r0 >=0 and r1<0:
after[r0:300,0:300+r1,0] = before[0:300-r0,-r1:300,0]
elif r0<0 and r1>=0:
after[0:300+r0,r1:300,0] = before[-r0:300,0:300-r1,0]
else:
after[0:300+r0,0:300+r1,0] = before[-r0:300,-r1:300,0]
if r2 >= 0 and r3 >= 0:
after[r2:300,r3:300,1:3] = before[0:300-r2,0:300-r3,1:3]
elif r2 >=0 and r3<0:
after[r2:300,0:300+r3,1:3] = before[0:300-r2,-r3:300,1:3]
elif r2<0 and r3>=0:
after[0:300+r2,r3:300,1:3] = before[-r2:300,0:300-r3,1:3]
else:
after[0:300+r2,0:300+r3,1:3] = before[-r2:300,-r3:300,1:3]
s = str(r0) + ":" + str(r1) + "," + str(r2) + ":" + str(r3)
cv2.putText(after,s,(0,80),cv2.FONT_HERSHEY_TRIPLEX,1,(255,255,255),1)
image_list.append(after)
imageio.mimsave("./test.gif",image_list,fps=1)
- 参数观察
- 这次制作了动图gif
- 可以设置
- fps
- duration
- loop
- 以前的动画
- 应该都可以保存成动图了
- 动画还有什么好玩的吗?🤔
- 下次再说👋