字符视频
Posted little-turtle--qjy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符视频相关的知识,希望对你有一定的参考价值。
图片 转 字符
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 += ‘
‘
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(‘{}
‘.format(line))
以上是关于字符视频的主要内容,如果未能解决你的问题,请参考以下文章