程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

I exported 20000 lines of characters in Python and made an animation

編輯:Python

I saw it the other day B standing Up Video of Mr. He , With the idea of exporting character files to make videos , It takes more time than I thought , It's really hard to get stuck …

But once you have an idea in your head , I feel bad if I don't come out …

This is a video ↓↓↓

I use Python Exported 20000 lines of characters , Made animation ...

B Station Homepage :https://space.bilibili.com/279911711

If the video is good , Ask for a favor to encourage ~

The document and code of character animation in the video are posted here , If you want to make a similar video, you can refer to ~~

1、 The preceding character effect

import os # File module 
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 = "●●●●●"
# effect 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: Song style '>"+'&nbsp;'*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: Song style '>"+'&nbsp;'*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: Song style '>"+'&nbsp;'*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: Song style '>"+'&nbsp;'*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: Song style '>"+'&nbsp;'*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: Song style '>"+'&nbsp;'*index + "<span style='font-size:40.0pt;'>" + default_texts[0]*repead + '</span>' + '</p>'
data += text + "\n"
snake1 += 1
# print("data",data)
# effect 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: Song style ';>"+'&nbsp;'*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>&nbsp;</p>" + "\n"
index +=1
snake2+=1
# effect 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: Song style ';>"+'&nbsp;'*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>&nbsp;</p>" + "\n"
index +=1
snake3+=1
# effect 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>' + '&nbsp;'*index
# print(time_text)
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ;'>"+ 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>' + '&nbsp;'*index
# print(time_text)
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ;'>"+time_text +"</p>"
div_text += text + "\n"
index -=1
color_index += 1
print(color_index)
data = data + div_text + "</div>"
snake4+=1
# # effect 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>' + '&nbsp;'*index
# # print(time_text)
# text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ 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>' +'&nbsp;'*index + time_text
# text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ time_text +"</p>"
# div_text += text + "\n"
# index -=1
# color_index += 1
# print(color_index)
# data += div_text + "</div>"
# snake5+=1
# effect 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>' + '&nbsp;'*index
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ 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>' +'&nbsp;'*index + time_text
index_length += index
time_index = blank3 - index_length
time_text = '&nbsp;'*time_index + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ 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>' + '&nbsp;'*index
index_length -= index
# print("index_length",index_length)
time_text = '&nbsp;'*index_length + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ 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>' +'&nbsp;'*index + time_text
text = "<p style='font-size:1.0pt;font-family:Helvetica;mso-bidi-font-family: Song style ';>"+ time_text +"</p>"
div_text += text + "\n"
index -=1
color_index += 1
print(color_index)
data += div_text + "</div>"
snake6+=1
# effect 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(' Write file successful ')

2、 Penguin animation in the back

The little penguin animation uses cv2 Computer vision plug-ins , Get to convert video into video frames , Parse into characters

import os
import sys
import numpy
import cv2 # Read video Computer vision 
gray_num = list(" ")
def main(videoPath: str):
# 1、 Get video 2、 Get video frames 3、 Loop through video frames 4、 Parse the picture into characters 
# Output 
data = ""
cap = cv2.VideoCapture(videoPath) # Read video 
while True:
success, frame = cap.read() # Read video frames 
if not success:
break
# Convert grayscale 
grayFrame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# Adjust the size 
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 Floating point numbers 
n = (p/255)*(len(gray_num)-1) # The position of gray pixels in the character list 
index = int(n) # Convert to integer 
text = gray_num[index]
# If you encounter non blank pixels ,flag+1
if text != " ":
flag += 1
if flag == 1:
row = row + "<span style='font-size:18px;line-height:1pt'>" + "&nbsp;"*blank + "</span>"
blank = 0
row += text
else:
flag = 0
blank += 1
asciiFrame = asciiFrame + "<p style='font-size:1.0pt;'>" + row[:-1] + "</p>" + "\n" # Character frame wrapping 
print(asciiFrame) # Output 
data = data + "<div style='height:719px;'>" + asciiFrame + "</div>"
cap.release()
path = os.getcwd()
file = open(path+"/txt.html","w")
file.write(data)
print(' Write file successful ')
if __name__ == "__main__":
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
main(sys.argv[1])
else:
print("No File")

3、 The screen slides down automatically

import pyautogui
while(1):
# Negative values scroll down , Positive values scroll up , If vision goes down , It's just a little slow , You should add some numbers 
# Penguin speed 
pyautogui.scroll(-18)
# effect 1 Ribbon speed 
# pyautogui.scroll(-11)

I put the document and code of character animation in the video in the following link , If you want to make a similar video, you can refer to ~~

link :
https://pan.baidu.com/s/10fUIkXkq8d1S8RcuTafnFQ
password :9cca


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved