码迷,mamicode.com
首页 > 其他好文 > 详细

字符视频

时间:2020-07-18 00:49:39      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:open   demo   text   odi   asc   port   str   turn   等于   

图片 转 字符

from PIL import Image

def main():
    for a in range(0, 100):
        print(a)
        if a < 10:
            pic = "001 - 2333000" + str(a) + ".jpg"
        if a >= 10 and a < 100:
            pic = "001 - 233300" + str(a) + ".jpg"
        if a >= 100 and a < 1000:
            pic = "001 - 23330" + str(a) + ".jpg"
        if a >= 1000:
            pic = "001 - 2333" + sta(a) + ".jpg"
        # 0000
        # 10个字符表示按“灰度级别”从高到低排序
        asciis = "@%#*+=-:. "
        # 设置缩放系数
        zoom = 1
        # 设置垂直比例系数
        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, 1477):
    #################
    sleep(0.1)#注入灵魂,画面稳定
    #################
    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))

字符视频

标签:open   demo   text   odi   asc   port   str   turn   等于   

原文地址:https://www.cnblogs.com/Little-Turtle--QJY/p/13333903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!