由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Opencv & gif

reference: https://blog.csdn.net/qq_41500251/article/details/82820806

  1. import time, cv2
  2. from PIL import Image
  3. import numpy as np
  4. def GIF(file):
  5. List = []
  6. im = Image.open(file)
  7. im.seek(1)#skip to the second frame
  8. try:
  9. while 1:
  10. List += [cv2.cvtColor(np.asarray(im.convert()),cv2.COLOR_RGB2BGR)]
  11. im.seek(im.tell()+1)
  12. except EOFError:#the sequence ends
  13. pass
  14. Num = 0
  15. while Num < len(List)*2:
  16. Num +=1
  17. ID = Num%(len(List))
  18. cv2.imshow("OpenCV",List[ID])
  19. print(ID)
  20. cv2.waitKey(1)
  21. time.sleep(0.1)
  22. cv2.destroyAllWindows()

for Videos:

  1. from cv2 import cv2
  2. import imageio
  3. import numpy
  4. # Collection of the imgs
  5. frames_list = []
  6. # Tossed frames per FPS. When FPS = 1, all frame are saved.
  7. FPS = 1
  8. cap=cv2.VideoCapture("test_1.mp4")
  9. while (True):
  10. ret,frame=cap.read()
  11. #img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  12. img = cv2.resize(frame, (460,360))
  13. frames_list.append(img)
  14. frames = []
  15. Num =0
  16. for img in frames_list:
  17. Num +=1
  18. if Num %3 == 0:
  19. frames.append(img)
  20. gif=imageio.mimsave('test_3.gif',frames,'GIF',duration=1/8)

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗