Advertisement
willtrieagain

image_to_video.py

Jan 17th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import cv2
  2. import argparse
  3. import os
  4.  
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument(
  7.     '-dir',
  8.     '-directory',
  9.     required=False,
  10.     default='ut',
  11.     help='specify location of image folder'
  12. )
  13. parser.add_argument(
  14.     '-n',
  15.     '-name',
  16.     required=False,
  17.     default='merged',
  18.     help='specify location of video'
  19. )
  20. parser.add_argument(
  21.     '-f',
  22.     '-framerate',
  23.     type=int,
  24.     default=30,
  25.     help='specify the frame rate'
  26. )
  27. args = parser.parse_args()
  28.  
  29. path = args.dir
  30. video_name = args.n + ".mp4"
  31. frame_rate = args.f
  32. images = os.listdir(path)
  33. images = [os.path.join(path, image) for image in images]
  34. images.sort(key=lambda x: os.path.getmtime(x))
  35.  
  36. img_path = images[-1]
  37. frame = cv2.imread(img_path)
  38. cv2.imshow('video',frame)
  39.  
  40. h, w = frame.shape[:2]
  41. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  42. writer = cv2.VideoWriter(video_name, fourcc, frame_rate, (w, h))
  43.  
  44. for img in images:
  45.     frame = cv2.imread(img)
  46.     writer.write(frame)
  47.     # cv2.imshow('video',frame)
  48.     if (cv2.waitKey(1) & 0xFF) == ord('q'):
  49.         break
  50. writer.release()
  51. cv2.destroyAllWindows()
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement