OpenCV制作视频字符画[Python]

OpenCV制作视频字符画[Python]

import cv2
import os
import time

import platform

show_heigth = 30
show_width = 80
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
#生成一个ascii字符列表
char_len = len(ascii_char)


def readVideo(videoPath):
    vc = cv2.VideoCapture(videoPath)
    if vc.isOpened():
        rval,frame = vc.read()
    else:
        rval = False
    frame_count = 0
    ouputList = []

    # fource = cv2.VideoWriter_fourcc(*'XVID')
    # out = cv2.VideoWriter('c.avi', fource, 20.0, (show_width, show_heigth), False)
    while rval:
       gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
       gray = cv2.resize(gray,(show_width,show_heigth))
       text = ""
       for gr in gray:
           for g in gr:
               text += ascii_char[int(g / 256 * char_len)]
           text+="\t"
       # out.write(gray)
       ouputList.append(text)
       frame_count+=1
       if frame_count%100==0:
           print("已处理%d帧"%frame_count)
       rval,frame = vc.read()

    vc.release()
    # out.release()
    cv2.destroyAllWindows()
    with open('c.txt','a+') as fs:
        for line in ouputList:
            fs.write(line+"\n")

    return ouputList

def playL(oList):
    ds = ["30","31","32","33","34","35","36","37"]
    i = 0
    c = ""
    for f in oList:
        i+=1
        if i%60==0:
            c = ds[int(i/10%8-1)]
        time.sleep(0.016)
        t = os.system("clear")
        f = f.replace('\t', '\n')
        print("\033[1;%sm%s"%(c,f),flush=True)
        print()
        print()
def playW(oList):
    cs = ["01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F"]
    i = 0
    c = ""
    for f in oList:
        i+=1
        if i%60==0:
            a = "color %s"%(cs[int(i / 10 % 15)-1])
            os.system(a)
        os.system("cls")
        time.sleep(0.016)
        f = f.replace('\t', '\n')
        print(f,flush=True)

        print()
        print()
if __name__ == '__main__':
    videoPath = ".\b.mp4"
    ouputLists = []
    #获取数据
    if os.path.exists("c.txt"):
        with open('c.txt','r') as fs:
            ouputLists = fs.readlines()
    else:
        ouputLists = readVideo(videoPath)
    #播放
    if platform.system()=="Windows":
        playW(ouputLists)
    else:
        playL(ouputLists)



效果展示

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值