KrYn0MoRe

ex2

Oct 27th, 2020
1,007
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WIDTH1 = 0
  2. HEIGHT1 = 0
  3. import re
  4. import os
  5. import time
  6. import cv2
  7. from colormap import rgb2hex
  8. from PIL import Image
  9. from shutil import move
  10.  
  11. mode = 2
  12.  
  13. def tobyte(r,g,b):
  14.     a = chr(r)+chr(g)+chr(b)
  15.     return a
  16.  
  17. FILEINPUT = input("Input video file. ")
  18. if not FILEINPUT or not os.path.exists(FILEINPUT):
  19.     print("Invalid video file.")
  20. else:
  21.     #
  22.     vid = cv2.VideoCapture(FILEINPUT)
  23.     height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
  24.     width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
  25.     div = float(input("Input multiplier size. "))
  26.     WIDTH1 = round(width*div)
  27.     HEIGHT1 = round(height*div)
  28.     bruh = True
  29.     c1 = input("Video will be " + str(HEIGHT1) + "x" + str(WIDTH1) + " resolution. Continue? (y/n)")
  30.     if str.lower(c1) == 'y':
  31.         print("Running..")
  32.         pass
  33.     else:
  34.         print("Quitting..")
  35.         bruh = False
  36.     #
  37.     while bruh:
  38.         time.sleep(1/10)
  39.         count = 0
  40.         while True:
  41.             #
  42.             time.sleep(1/10)
  43.             count += 1
  44.             ret, frame = vid.read()
  45.             if not ret:
  46.                 break
  47.             new_im = cv2.resize(frame,(WIDTH1, HEIGHT1),fx=0,fy=0,interpolation=cv2.INTER_CUBIC)
  48.             new_im = cv2.cvtColor(new_im, cv2.COLOR_BGR2RGB)
  49.             npd = ''
  50.             for y in new_im:
  51.                 for x in y:
  52.                     r,g,b = x
  53.                     if mode == 2: #hex compression
  54.                         npd = npd + rgb2hex(r,g,b)
  55.                     elif mode == 3: #byte compression
  56.                         npd = npd + tobyte(r,g,b)
  57.                     else: #grayscale compression
  58.                         npd = npd + str("{:03d}".format(r+g+b))
  59.             if mode == 3:
  60.                 npd = npd.encode('utf8')
  61.                 pixf = open("data_r.json", "wb+")
  62.             elif mode == 2:
  63.                 npd = npd.replace('#', '', len(npd))
  64.                 pixf = open("data_r.json", "w+")
  65.             else:
  66.                 pixf = open("data_r.json", "w+")
  67.             sizef = open("size_r.json", "w+")
  68.             st = [WIDTH1, HEIGHT1]
  69.             fst = re.sub('[ ]', '', str(st))
  70.             pixf.write(npd)
  71.             sizef.write(fst)
  72.             pixf.close()
  73.             sizef.close()
  74.             f = 'data_r.json'
  75.             move(f,f.replace('_r.json','.json'))
  76.             f2 = 'size_r.json'
  77.             move(f2,f2.replace('_r.json','.json'))
  78.             #
  79.             print("Success frame: " + str(count))
  80.  
RAW Paste Data