我用Python导出了两万行字符,做成了动画...
程序员文章站
2022-07-13 09:14:41
...
前几天看了B站Up主何同学的视频,有了这个导出字符文档做成视频想法,比我想象中要费时间,卡点实在是太难了…
但是一旦脑子里有个想法,不实现出来浑身难受…
这是视频 ↓↓↓
我用Python导出了两万行字符,做成了动画...
如果感觉视频做的还不错,求点个赞鼓励一下~
视频中的字符动画的文档和代码我这这里贴一下,大家想要做类似视频的话可以参考~~
1、前面的字符效果
import os #文件模块
import random
# "♡"
snake1,snake2,snake3,snake4,snake5,snake6 = 0,0,0,0,0,0
data = ""
blank = 600
blank2 = 600
blank3 = 800
default_texts = ["o","■","□"]
repead = 6
repead2 = 5
repead3 = 8
speed = 5
food = "●●●●●"
# 效果1
while snake1 < 4:
index = 0
while index <= blank:
# print(index)
index += speed
text = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*repead + '</span>' + '</p>'
data += text + "\n"
while index+speed >= blank:
index += (speed//2)
if index+(speed//2) > blank:
# print(index)
for i in [1,2,3,4,5]:
index += 1
text = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*(repead+i) + '</span>' + '</p>'
data += text + "\n"
else:
text = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*repead + '</span>' + '</p>'
data += text + "\n"
if index > blank:
food_data = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*(repead//2) + "<span style='color:orange;'>" + food + "</span>" + default_texts[0]*(repead//2) + "</span></p>"
for i in [1,2,3,4,5]:
data += food_data + "\n"
break
while index >= 0:
# print("index",index)
while index+(speed//2) > blank:
# print("index22",index)
index -= (speed//2)
for i in [5,4,3,2,1]:
index -= 1
text = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*(repead+i) + '</span>' + '</p>'
# print(text)
data += text + "\n"
if index < 0:
break
index -= speed
text = "<p style='font-size:1.0pt;color:red;font-family:Helvetica;mso-bidi-font-family:宋体'>"+' '*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*repead + '</span>' + '</p>'
data += text + "\n"
snake1 += 1
# print("data",data)
# 效果2
while snake2 < 50:
index = 0
if snake2 % 2 == 1:
time_blank = random.randint(0,(blank2//2))
else:
time_blank = random.randint((blank2//2),blank2)
while index < 6:
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+' '*time_blank + "<span style='font-size:20.0pt;'>" + default_texts[1]*repead2 + '</span>' + '</p>'
data += text + "\n"
index +=1
index = 0
while index < 3:
data += "<p> </p>" + "\n"
index +=1
snake2+=1
# 效果3
while snake3 < 50:
index = 0
default_text = random.choice(default_texts[1:])
if snake3 % 2 == 1:
time_blank = random.randint(0,(blank2//2))
else:
time_blank = random.randint((blank2//2),blank2)
while index < 6:
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+' '*time_blank + "<span style='font-size:20.0pt;'>" + default_text*repead2 + '</span>' + '</p>'
data += text + "\n"
index +=1
index = 0
while index < 3:
data += "<p> </p>" + "\n"
index +=1
snake3+=1
# 效果4
color_index = 1
while snake4 < 15:
index = 0
default_text = default_texts[0]
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index <= blank3//repead3:
time_text = ""
for i in range(1, (repead3+1)):
time_text += "<span style='font-size:20.0pt;'>" + default_text + '</span>' + ' '*index
# print(time_text)
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体;'>"+ time_text +"</p>"
div_text += text + "\n"
index +=1
color_index += 1
print(color_index)
data = data + div_text + "</div>"
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index >= 0:
time_text = ""
for i in range(1, (repead3+1)):
time_text += "<span style='font-size:20.0pt;'>" + default_text + '</span>' + ' '*index
# print(time_text)
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体;'>"+time_text +"</p>"
div_text += text + "\n"
index -=1
color_index += 1
print(color_index)
data = data + div_text + "</div>"
snake4+=1
# # 效果5
# while snake5 < 20:
# index = 0
# default_text = default_texts[0]
# div_text = "<div class='simple-linear" + str(color_index) + "'>"
# while index <= blank3//repead3:
# time_text = ""
# for i in range(1, (repead3+1)):
# time_text += "<span style='font-size:20.0pt;'>" + default_text + '</span>' + ' '*index
# # print(time_text)
# text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
# div_text += text + "\n"
# index +=1
# color_index += 1
# print(color_index)
# data += div_text + "</div>"
# div_text = "<div class='simple-linear" + str(color_index) + "'>"
# while index >= 0:
# time_text = ""
# for i in range(1, (repead3+1)):
# time_text = "<span style='font-size:20.0pt;'>" + default_text + '</span>' +' '*index + time_text
# text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
# div_text += text + "\n"
# index -=1
# color_index += 1
# print(color_index)
# data += div_text + "</div>"
# snake5+=1
# 效果6
while snake6 < 7:
index = 0
default_text = default_texts[0]
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index <= blank3//repead3:
time_text = ""
for i in range(1, (repead3+1)):
time_text += "<span style='font-size:20.0pt;'>" + default_text + '</span>' + ' '*index
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
div_text += text + "\n"
index +=1
color_index += 1
print(color_index)
data += div_text + "</div>"
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index >= 0:
time_text = ""
index_length = 0
for i in range(1, (repead3+1)):
time_text = "<span style='font-size:20.0pt;'>" + default_text + '</span>' +' '*index + time_text
index_length += index
time_index = blank3 - index_length
time_text = ' '*time_index + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
div_text += text + "\n"
index -=1
color_index += 1
print(color_index)
data += div_text + "</div>"
index = 0
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index <= blank3//repead3:
time_text = ""
index_length = blank3
for i in range(1, (repead3+1)):
time_text += "<span style='font-size:20.0pt;'>" + default_text + '</span>' + ' '*index
index_length -= index
# print("index_length",index_length)
time_text = ' '*index_length + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
div_text += text + "\n"
index +=1
color_index += 1
print(color_index)
data += div_text + "</div>"
index = blank3//repead3
div_text = "<div class='simple-linear" + str(color_index) + "'>"
while index >= 0:
time_text = ""
# index_length = 0
for i in range(1, (repead3+1)):
time_text = "<span style='font-size:20.0pt;'>" + default_text + '</span>' +' '*index + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family:宋体';>"+ time_text +"</p>"
div_text += text + "\n"
index -=1
color_index += 1
print(color_index)
data += div_text + "</div>"
snake6+=1
# 效果7
# http://www.divcss5.com/peise/
style = '''<style>* {margin:0;padding:0;}
.simple-linear1{ background: linear-gradient(#FFFFFF,#FFCC99);}
.simple-linear2{ background: linear-gradient(#FFCC99,#FFFF99);}
.simple-linear3{ background: linear-gradient(#FFFF99,#99CC99);}
.simple-linear4{ background: linear-gradient(#99CC99,#FFCC99);}
.simple-linear5{ background: linear-gradient(#FFCC99,#CCFF99);}
.simple-linear6{ background: linear-gradient(#CCFF99,#CCCCCC);}
.simple-linear7{ background: linear-gradient(#CCCCCC,#FFCC99);}
.simple-linear8{ background: linear-gradient(#FFCC99,#FFFFCC);}
.simple-linear9{ background: linear-gradient(#FFFFCC,#99CCFF);}
.simple-linear10{ background: linear-gradient(#99CCFF,#FF9966);}
.simple-linear11{ background: linear-gradient(#FF9966,#FFFFCC);}
.simple-linear12{ background: linear-gradient(#FFFFCC,#99CC99);}
.simple-linear13{ background: linear-gradient(#99CC99,#FF9900);}
.simple-linear14{ background: linear-gradient(#FF9900,#FFFFCC);}
.simple-linear15{ background: linear-gradient(#FFFFCC,#FF6666);}
.simple-linear16{ background: linear-gradient(#FF6666,#CCCC33);}
.simple-linear17{ background: linear-gradient(#CCCC33,#FFFF99);}
.simple-linear18{ background: linear-gradient(#FFFF99,#CC9933);}
.simple-linear19{ background: linear-gradient(#CC9933,#996600);}
.simple-linear20{ background: linear-gradient(#996600,#FFCC33);}
.simple-linear21{ background: linear-gradient(#FFCC33,#009966);}
.simple-linear22{ background: linear-gradient(#009966,#FFFFCC);}
.simple-linear23{ background: linear-gradient(#FFFFCC,#CC9933);}
.simple-linear24{ background: linear-gradient(#CC9933,#FF6666);}
.simple-linear25{ background: linear-gradient(#FF6666,#FF9900);}
.simple-linear26{ background: linear-gradient(#FF9900,#FFFF00);}
.simple-linear27{ background: linear-gradient(#FFFF00,#0099CC);}
.simple-linear28{ background: linear-gradient(#0099CC,#99CC33);}
.simple-linear29{ background: linear-gradient(#99CC33,#FF9900);}
.simple-linear30{ background: linear-gradient(#FF9900,#FFCC00);}
.simple-linear31{ background: linear-gradient(#FFCC00,#FF9933);}
.simple-linear32{ background: linear-gradient(#FF9933,#99CC33);}
.simple-linear33{ background: linear-gradient(#99CC33,#CC6699);}
.simple-linear34{ background: linear-gradient(#CC6699,#FF9933);}
.simple-linear35{ background: linear-gradient(#FF9933,#FFFF00);}
.simple-linear36{ background: linear-gradient(#FFFF00,#3366CC);}
.simple-linear37{ background: linear-gradient(#3366CC,#FF9933);}
.simple-linear38{ background: linear-gradient(#FF9933,#FFFFCC);}
.simple-linear39{ background: linear-gradient(#FFFFCC,#009966);}
.simple-linear40{ background: linear-gradient(#009966,#FF6600);}
.simple-linear41{ background: linear-gradient(#FF6600,#FFFF66);}
.simple-linear42{ background: linear-gradient(#FFFF66,#009966);}
.simple-linear43{ background: linear-gradient(#009966,#990033);}
.simple-linear44{ background: linear-gradient(#990033,#CCFF66);}
.simple-linear45{ background: linear-gradient(#CCFF66,#FF9900);}
.simple-linear46{ background: linear-gradient(#FF9900,#FF9966);}
.simple-linear47{ background: linear-gradient(#FF9966,#996600);}
.simple-linear48{ background: linear-gradient(#996600,#CCCC00);}
.simple-linear49{ background: linear-gradient(#CCCC00,#CC6600);}
.simple-linear50{ background: linear-gradient(#CC6600,#999999);}
.simple-linear51{ background: linear-gradient(#999999,#CCCC33);}
.simple-linear52{ background: linear-gradient(#CCCC33,#CC6600);}
.simple-linear53{ background: linear-gradient(#CC6600,#CCCC33);}
.simple-linear54{ background: linear-gradient(#CCCC33,#336699);}
.simple-linear55{ background: linear-gradient(#336699,#CC3366);}
.simple-linear56{ background: linear-gradient(#CC3366,#FF9933);}
.simple-linear57{ background: linear-gradient(#FF9933,#999966);}
.simple-linear58{ background: linear-gradient(#999966,#663300);}
.simple-linear59{ background: linear-gradient(#663300,#FF9933);}
.simple-linear60{ background: linear-gradient(#FF9933,#FFFF66);}
.simple-linear61{ background: linear-gradient(#FFFF66,#990066);}
.simple-linear62{ background: linear-gradient(#990066,#FFCC00);}
.simple-linear63{ background: linear-gradient(#FFCC00,#CC0033);}
.simple-linear64{ background: linear-gradient(#CC0033,#FFCC33);}
.simple-linear65{ background: linear-gradient(#FFCC33,#333399);}
.simple-linear66{ background: linear-gradient(#333399,#FF0033);}
.simple-linear67{ background: linear-gradient(#FF0033,#666699);}
.simple-linear68{ background: linear-gradient(#666699,#FFFF00);}
.simple-linear69{ background: linear-gradient(#FFFF00,#CC3366);}
.simple-linear70{ background: linear-gradient(#CC3366,#FF0033);}
.simple-linear71{ background: linear-gradient(#FF0033,#006699);}
.simple-linear72{ background: linear-gradient(#006699,#FFFF33);}
.simple-linear73{ background: linear-gradient(#FFFF33,#FFCC00);}
.simple-linear74{ background: linear-gradient(#FFCC00,#009999);}
.simple-linear75{ background: linear-gradient(#009999,#CC3366);}
.simple-linear76{ background: linear-gradient(#CC3366,#FF0033);}
.simple-linear77{ background: linear-gradient(#FF0033,#CCCC00);}
.simple-linear78{ background: linear-gradient(#CCCC00,#006699);}
.simple-linear79{ background: linear-gradient(#006699,#CCCC00);}
.simple-linear80{ background: linear-gradient(#CCCC00,#FF9933);}
.simple-linear81{ background: linear-gradient(#FF9933,#663399);}
.simple-linear82{ background: linear-gradient(#663399,#FF9933);}
.simple-linear83{ background: linear-gradient(#FF9933,#FFFF00);}
.simple-linear84{ background: linear-gradient(#FFFF00,#336699);}
.simple-linear85{ background: linear-gradient(#336699,#CC3333);}
.simple-linear86{ background: linear-gradient(#CC3333,#FFCCCC);}
.simple-linear87{ background: linear-gradient(#FFCCCC,#99CC00);}
.simple-linear88{ background: linear-gradient(#99CC00,#003399);}
.simple-linear89{ background: linear-gradient(#003399,#FFFF00);}
.simple-linear90{ background: linear-gradient(#FFFF00,#FF6600);}
.simple-linear91{ background: linear-gradient(#FF6600,#FFCC99);}
.simple-linear92{ background: linear-gradient(#FFCC99,#FFFF99);}
.simple-linear93{ background: linear-gradient(#FFFF99,#99CC99);}
.simple-linear94{ background: linear-gradient(#99CC99,#FFCC99);}
.simple-linear95{ background: linear-gradient(#FFCC99,#CCFF99);}
.simple-linear96{ background: linear-gradient(#CCFF99,#CCCCCC);}
.simple-linear97{ background: linear-gradient(#CCCCCC,#FFCC99);}
.simple-linear98{ background: linear-gradient(#FFCC99,#FFFFCC);}
.simple-linear99{ background: linear-gradient(#FFFFCC,#99CCFF);}
.simple-linear100{ background: linear-gradient(#99CCFF,#FF9966);}
.simple-linear101{ background: linear-gradient(#FF9966,#FFFFCC);}
.simple-linear102{ background: linear-gradient(#FFFFCC,#99CC99);}
.simple-linear103{ background: linear-gradient(#99CC99,#FF9900);}
.simple-linear104{ background: linear-gradient(#FF9900,#FFFFCC);}
.simple-linear105{ background: linear-gradient(#FFFFCC,#336699);}
.simple-linear106{ background: linear-gradient(#336699,#CCCC33);}
.simple-linear107{ background: linear-gradient(#CCCC33,#FFFF99);}
.simple-linear108{ background: linear-gradient(#FFFF99,#CC9933);}
.simple-linear109{ background: linear-gradient(#CC9933,#996600);}
.simple-linear110{ background: linear-gradient(#996600,#FFCC33);}
.simple-linear111{ background: linear-gradient(#FFCC33,#FF9900);}
.simple-linear112{ background: linear-gradient(#FF9900,#FFFFCC);}
.simple-linear113{ background: linear-gradient(#FFFFCC,#CC9933);}
.simple-linear114{ background: linear-gradient(#CC9933,#FF6666);}
.simple-linear115{ background: linear-gradient(#FF6666,#FF9900);}
.simple-linear116{ background: linear-gradient(#FF9900,#FFFF00);}
.simple-linear117{ background: linear-gradient(#FFFF00,#0099CC);}
.simple-linear118{ background: linear-gradient(#0099CC,#99CC33);}
.simple-linear119{ background: linear-gradient(#99CC33,#FF9900);}
.simple-linear120{ background: linear-gradient(#FF9900,#FFCC00);}
.simple-linear121{ background: linear-gradient(#FFCC00,#FF9933);}
.simple-linear122{ background: linear-gradient(#FF9933,#99CC33);}
.simple-linear123{ background: linear-gradient(#99CC33,#CC6699);}
.simple-linear124{ background: linear-gradient(#CC6699,#FF9933);}
.simple-linear125{ background: linear-gradient(#FF9933,#FFFF00);}
.simple-linear126{ background: linear-gradient(#FFFF00,#3366CC);}
.simple-linear127{ background: linear-gradient(#3366CC,#FF9933);}
.simple-linear128{ background: linear-gradient(#FF9933,#FFFFCC);}
.simple-linear129{ background: linear-gradient(#FFFFCC,#009966);}
.simple-linear130{ background: linear-gradient(#009966,#FF6600);}
.simple-linear131{ background: linear-gradient(#FF6600,#FFFF66);}
.simple-linear132{ background: linear-gradient(#FFFF66,#009966);}
.simple-linear133{ background: linear-gradient(#009966,#990033);}
.simple-linear134{ background: linear-gradient(#990033,#CCFF66);}
.simple-linear135{ background: linear-gradient(#CCFF66,#FF9900);}
.simple-linear136{ background: linear-gradient(#FF9900,#FF9966);}
.simple-linear137{ background: linear-gradient(#FF9966,#996600);}
.simple-linear138{ background: linear-gradient(#996600,#CCCC00);}
.simple-linear139{ background: linear-gradient(#CCCC00,#CC6600);}
.simple-linear140{ background: linear-gradient(#CC6600,#999999);}
.simple-linear141{ background: linear-gradient(#999999,#CCCC33);}
.simple-linear142{ background: linear-gradient(#CCCC33,#CC6600);}
.simple-linear143{ background: linear-gradient(#CC6600,#CCCC33);}
.simple-linear144{ background: linear-gradient(#CCCC33,#336699);}
.simple-linear145{ background: linear-gradient(#336699,#000000);}
.simple-linear146{ background: linear-gradient(#000000,#FF9933);}
.simple-linear147{ background: linear-gradient(#FF9933,#999966);}
.simple-linear148{ background: linear-gradient(#999966,#663300);}
.simple-linear149{ background: linear-gradient(#663300,#FF9933);}
.simple-linear150{ background: linear-gradient(#FF9933,#FFFF66);}
</style>'''
data = style + data
path = os.getcwd()
file = open(path+"/snake.html","w")
file.write(data)
print('写入文件成功')
2、后面的企鹅动画
小企鹅的动画用的是cv2计算机视觉插件,获取将视频转成视频帧,解析成字符
import os
import sys
import numpy
import cv2 # 读取视频 计算机视觉
gray_num = list("▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ")
def main(videoPath: str):
# 1、获取视频 2、获取视频帧 3、循环处理视频帧 4、将图片解析成字符
# 输出
data = ""
cap = cv2.VideoCapture(videoPath) # 读取视频
while True:
success, frame = cap.read() # 读取视频帧
if not success:
break
# 转换灰度图
grayFrame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# 调整尺寸
tsize = os.get_terminal_size()
fixFrame = cv2.resize(grayFrame, (tsize.columns, int(tsize.lines)))
img_array = numpy.array(fixFrame, "f")
asciiFrame = ""
for line in img_array:
row = ""
blank = 0
flag = 0
for p in line:
# p 浮点数数字
n = (p/255)*(len(gray_num)-1) # 灰度像素在字符列表中的位置
index = int(n) # 转换为整数
text = gray_num[index]
#如果碰到非空白像素,flag+1
if text != " ":
flag += 1
if flag == 1:
row = row + "<span style='font-size:18px;line-height:1pt'>" + " "*blank + "</span>"
blank = 0
row += text
else:
flag = 0
blank += 1
asciiFrame = asciiFrame + "<p style='font-size:1.0pt;'>" + row[:-1] + "</p>" + "\n" # 字符画帧换行
print(asciiFrame) # 输出
data = data + "<div style='height:719px;'>" + asciiFrame + "</div>"
cap.release()
path = os.getcwd()
file = open(path+"/txt.html","w")
file.write(data)
print('写入文件成功')
if __name__ == "__main__":
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
main(sys.argv[1])
else:
print("No File")
3、屏幕自动下滑代码
import pyautogui
while(1):
# 负值为向下滚动,正值向上滚动,如果视觉往下走,就是有点慢,应该加点数值
# 企鹅速度
pyautogui.scroll(-18)
# 效果1丝带速度
# pyautogui.scroll(-11)
视频中的字符动画的文档和代码我都放到下面链接中了,大家想要做类似视频的话可以参考~~
上一篇: ES6 Promise、Symbol、async函数
下一篇: 使用Ansible离线安装TIDB