I wrote a small project to render video in the console. I used PyCharm, and if I run the code in it everything works as it should, but how do I run the file so that the result is displayed on the console?
import cv2 as cv
import numpy as np
import os
vid = cv.VideoCapture('badapple.mp4')
input()
ret, frame = vid.read()
a = frame.shape[0] / 50
b = frame.shape[1]//a*2
b = "mode con cols="+str(int(b))+" lines=50"
os.system(b)
while 1:
    cons_frame = ''
    ret, frame = vid.read()
    new_frame1 = cv.resize(frame, (int(frame.shape[1]//a*2), 50), interpolation=cv.INTER_AREA)
    cv.imshow('title', frame)
    new_frame = cv.cvtColor(new_frame1, cv.IMREAD_GRAYSCALE)
    for i in new_frame:
        for j in i:
            if j[2] < 25:
                cons_frame = cons_frame + ' '
            elif j[2] < 50:
                cons_frame = cons_frame + '.'
            elif j[2] < 75:
                cons_frame = cons_frame + '-'
            elif j[2] < 100:
                cons_frame = cons_frame + ':'
            elif j[2] < 125:
                cons_frame = cons_frame + '='
            elif j[2] < 150:
                cons_frame = cons_frame + '+'
            elif j[2] < 175:
                cons_frame = cons_frame + 'x'
            elif j[2] < 200:
                cons_frame = cons_frame + '#'
            elif j[2] < 225:
                cons_frame = cons_frame + '%'
            else:
                cons_frame = cons_frame + '@'
    print(cons_frame, flush=True)
    if cv.waitKey(20) & 0xFF == ord('d'):
        break
vid.release()
cv.destroyAllWindows()
Advertisement
Answer
Judging from your usage of mode con, you must be on Windows. Assuming you have Python installed correctly, it should be as simple as opening up cmd.exe, navigating to your project’s .py file, and typing
python badapple.py, or whatever the script’s filename is.