Advertisement
MagicWinnie

Terminal Video Player

Jul 9th, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import sys
  2. import os
  3. import time
  4. from argparse import ArgumentParser
  5. import numpy as np
  6. import cv2
  7. from numpy.testing._private.utils import tempdir
  8. from colorparams import color
  9.  
  10. def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
  11.     dim = None
  12.     (h, w) = image.shape[:2]
  13.     if width is None and height is None:
  14.         return image
  15.     if width is None:
  16.         r = height / float(h)
  17.         dim = (int(w * r), height)
  18.     else:
  19.         r = width / float(w)
  20.         dim = (width, int(h * r))
  21.     resized = cv2.resize(image, dim, interpolation=inter)
  22.     return resized
  23.  
  24. def frame_to_ascii_grayscale(img):
  25.     chars = np.asarray(list("@%#*+=-:. "))
  26.    
  27.     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  28.     img = (img / 255.) * (len(chars) - 1)
  29.  
  30.     out = ""
  31.     out_arr = chars[img.astype(int)]
  32.     for i, r in enumerate(out_arr):
  33.         out += "".join(r) + '\n'
  34.  
  35.     return out
  36.  
  37. def render_frame(ascii_img, terminal_size):
  38.     sys.stdout.write("\033[{}F".format(terminal_size.lines - 2))
  39.     ascii_img = ascii_img.rstrip('\n')
  40.     print(ascii_img)
  41.  
  42. parser = ArgumentParser()
  43. parser.add_argument("-i", "--image", help="path to image file", type=str)
  44. parser.add_argument("-v", "--video", help="path to video file", type=str)
  45. parser.add_argument("-c", "--color", help="grayscale or colored output", type=bool)
  46. args = parser.parse_args()
  47.  
  48. if not args.image and not args.video:
  49.     print("Give one of two paths")
  50.     print(parser.print_help())
  51.     exit(-1)
  52. if args.image and args.video:
  53.     print("Give one of two paths!")
  54.     print(parser.print_help())
  55.     exit(-1)
  56.  
  57. terminal_size = os.get_terminal_size()
  58. os.system('cls' if os.name == 'nt' else 'clear')
  59.  
  60. if args.image:
  61.     img = cv2.imread(args.image)
  62.     assert img is not None, "Give a valid path to the image"
  63.  
  64.     if img.shape[1] > img.shape[0]:
  65.         img = image_resize(img, height=terminal_size.lines - 2)
  66.     else:
  67.         img = image_resize(img, width=terminal_size.width)
  68.    
  69.     frame = frame_to_ascii_grayscale(img)
  70.     render_frame(frame, terminal_size)
  71. else:
  72.     cap = cv2.VideoCapture(args.video)
  73.     assert cap.isOpened(), "Error opening video file"
  74.  
  75.     while cap.isOpened():
  76.         ret, img = cap.read()
  77.         if not ret:
  78.             break
  79.        
  80.         img = cv2.resize(img, (img.shape[1], img.shape[0] // 2))
  81.  
  82.         if img.shape[1] > img.shape[0]:
  83.             img = image_resize(img, height=terminal_size.lines - 2)
  84.         else:
  85.             img = image_resize(img, width=terminal_size.width)
  86.  
  87.         frame = frame_to_ascii_grayscale(img)
  88.         render_frame(frame, terminal_size)
  89.  
  90.     cap.release()
  91.     cv2.destroyAllWindows()
  92.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement