字符视频

视频 转 图片

import cv2
path = '/Users/xiaojiayudeapple/Desktop/未命名文件夹 2/002 - 蔡徐坤打篮球原视频.mp4'#路径
cp = cv2.VideoCapture(path)
s = cp.isOpened()
count = 0
while s:
    count += 1
    s, frame = cp.read()
    params = []
    params.append(2)
    cv2.imwrite('frames\\%d.jpg' % count, frame, params)

cp.release()
print('unlock movie: ', count)

图片 转 字符

from PIL import Image

def main():
    for a in range(1385, 2947):
        #金坷垃0000.jpg
        print(a)
        if a < 10:
            pic = "金坷垃000" + str(a) + ".jpg"
        if a >= 10 and a < 100:
            pic = "金坷垃00" + str(a) + ".jpg"
        if a >= 100 and a < 1000:
            pic = "金坷垃0" + str(a) + ".jpg"
        if a >= 1000:
            pic = "金坷垃" + str(a) + ".jpg"
        # 0000
        # 10个字符表示按“灰度级别”从高到低排序
        asciis = "@%#*+=-:. "
        # 设置缩放系数
        zoom = 2
        # 设置垂直比例系数
        vscale = 1
        texts = pic2ascii(pic, asciis, zoom, vscale)
        
        with open(str(a)+".txt", "w") as file:
            file.write(texts)

def pic2ascii(pic_, asciis, zoom, vscale):
    img = Image.open(pic_)
    # 打开图片并转换为灰度模式
    out = img.convert("L")
    # 获取图片的宽度和高度
    width, height = out.size
    # 由于字符的宽度并不会等于高度,所以需要进行调整
    out = out.resize((int(width * zoom), int(height * zoom * vscale)))
    ascii_len = len(asciis)
    texts = ''

    for row in range(out.height):
        for col in range(out.width):
            gray = out.getpixel((col, row))
            texts += asciis[int((gray / 255) * (ascii_len - 1))]
        texts += '\n'

    return texts

if __name__ == "__main__":
    main()

输出

import sys
from time import sleep

for a in range(0, 3030):
    sleep(0.3)
    with open(str(a) + '.txt','r',encoding='utf-8') as f:
        for line in f.readlines():
            # end=''控制文本中换行时不读取出换行号
            print(line,end='')
    # 定义列表
    ls = ["sunny","dghahdfg"]
    with open('demo.txt','a',encoding='utf-8') as f:
        for line in ls:
            # 写入文件
            f.write('{}\n'.format(line))
posted @ 2020-07-17 22:37  LT-Y  阅读(136)  评论(0编辑  收藏  举报