import wave # 导入 wave 模块,用于处理 WAV 音频文件
from random import randint # 从 random 模块导入 randint 函数,用于生成随机整数
def randomAudioFiles():
# 用于存储生成的音频文件名的列表
infiles = []
count = 1 # 初始化计数器
# 创建包含 16 个随机选择小节的列表,用于小步舞曲(Minuet)
while count < 17:
file = "waves/M" + str(count) # 构造文件名前缀,例如 "waves/M1"
rollOne = randint(1, 6) # 模拟第一次掷骰子(1 到 6)
rollTwo = randint(1, 6) # 模拟第二次掷骰子(1 到 6)
rollTotal = rollOne + rollTwo # 两次点数之和决定小节编号
file += "-" + str(rollTotal) + ".wav" # 构造完整的文件名,例如 "waves/M1-7.wav"
infiles.append(file) # 将生成的文件名加入列表
count += 1 # 计数器加一,处理下一个小节
# 创建包含 16 个随机选择小节的列表,用于三重奏部分(Trio)
while count < 33:
file = "waves/T" + str(count) # 构造文件名前缀,例如 "waves/T17"
roll = randint(1, 6) # 模拟一次掷骰子(1 到 6),决定三重奏小节编号
file += "-" + str(roll) + ".wav" # 构造完整的文件名,例如 "waves/T17-3.wav"
infiles.append(file) # 将生成的文件名添加到列表中
count += 1 # 计数器加一,直到生成 16 个文件名(T17 到 T32)
return infiles # 返回包含所有随机音频文件名的列表
def audioGenerator():
# 获取随机生成的音频文件名列表,设置输出文件名
infiles = randomAudioFiles()
outfile = "dice.wav"
data = []
for infile in infiles:
w = wave.open(infile, 'rb') # 打开一个 .wav 文件进行读取
print(w.getparams()) # 打印音频文件的参数信息(如声道数、采样率等)
# 读取参数和所有音频帧,保存为列表形式
data.append([w.getparams(), w.readframes(w.getnframes())])
w.close() # 关闭文件
# 写入输出文件,生成合并后的 .wav 音频文件
output = wave.open(outfile, 'wb') # 以写入模式创建输出文件
output.setparams(data[0][0]) # 设置音频参数,采用第一个输入文件的参数
for val in data:
output.writeframes(val[1]) # 写入每段音频的帧数据
output.close() # 关闭输出文件
return infiles # 返回所使用的输入文件名列表
pip install (安装文件的绝对路径)
xcode-select --install
pip install wordcloud
from wordcloud import WordCloud
string = 'Importance of relative word frequencies for font-size.\
With relative_scaling=0, only word-ranks are considered. With \
relative_scaling=1, a word that is twice as frequent will have \
twice the size. If you want to consider the word frequencies \
and not only their rank, relative_scaling around .5 often looks good.'
font = r'C:\Windows\Fonts\Arial.TTF'
wc = WordCloud(font_path=font, # Chinese
background_color='white',
width=1000,
height=800,
).generate(string)
wc.to_file('s1.png') # save figure
from matplotlib import pyplot as plt
plt.imshow(wc) # show figure by plt
plt.axis('off')
plt.show()
from wordcloud import WordCloud
filename = 'Harry Potter.txt'
with open(filename, encoding="utf-8") as f_obj:
contents = f_obj.read()
#font = r'C:\Windows\Fonts\Arial.TTF'
font = "/System/Library/Fonts/STHeiti Medium.ttc"
wc = WordCloud(font_path=font,
background_color='white',
width=1000,
height=800,
).generate(contents)
wc.to_file('s2.png')
from wordcloud import WordCloud
filename = 'sanguo.txt'
with open(filename , encoding="utf-8") as f_obj:
contents = f_obj.read()
#font = r'C:\Windows\Fonts\Arial.TTF'
font = "/System/Library/Fonts/STHeiti Medium.ttc"
wc = WordCloud(font_path=font,
background_color='white',
width=1000,
height=800,
).generate(contents)
wc.to_file('s3.png')
from wordcloud import WordCloud
import jieba
filename = 'sanguo.txt'
with open(filename , encoding="utf-8") as f_obj:
contents = f_obj.read()
s = jieba.lcut(contents)
txt = " ".join(s)
#font = r'C:\Windows\Fonts\Arial.TTF'
font = "/System/Library/Fonts/STHeiti Medium.ttc"
wc = WordCloud(font_path=font,
background_color='white',
width=1000,
height=800,
).generate(txt)
wc.to_file('s4.png')
from wordcloud import WordCloud
import jieba
filename = 'sanguo.txt'
with open(filename, encoding="utf-8") as f_obj:
contents = f_obj.read()
def sw(filename):
with open(filename, encoding="utf-8") as f_obj:
x = f_obj.readlines()
y = [word.strip() for word in x]
return y
name_list = ['baidu_stopwords.txt', 'cn_stopwords.txt',
'hit_stopwords.txt','scu_stopwords.txt']
stop_word = []
for x in name_list:
stop_word.extend(sw(x))
stop_word = list(set(stop_word))
s = jieba.lcut(contents)
result = [word for word in s if word not in stop_word]
s = [word for word in result if len(word)>1]
txt = " ".join(s)
#font = r'C:\Windows\Fonts\Arial.TTF'
font = "/System/Library/Fonts/STHeiti Medium.ttc"
wc = WordCloud(font_path=font,
background_color='white',
width=1000,
height=800,
).generate(txt)
wc.to_file('s5.png')
from moviepy.editor import *
clip = VideoFileClip("ultraman.flv")
clip = clip.subclip(0, 8) #剪切前8秒
clip.write_gif("video.gif") #保存为gif
import os
exist = os.path.exists('images') # 判断是否存在文件夹
if not exist:
os.mkdir('images')
else:
pass
from PIL import Image
#将gif的每一帧保存为png图片到images文件夹
def get_imgs():
gif = Image.open('video.gif')
try:
gif.save(f"images/{gif.tell()}.png")
while True:
gif.seek(gif.tell()+1)
gif.save(f"images/{gif.tell()}.png")
except Exception as e:
print("finished")
get_imgs()
# 设置要用的ascii码
ASCII_CHAR = "$@B%8&WM#*oahkbdpqwmZO0QLaCJUYXzczjhdhsdavunxr \
jft/\|()1{}[]?-_+~<>i!lI;:,\"^`'."
def rgb_to_ascii(r, g, b, alpha=256):
# 通过灰度值的映射
# 将没一个rgb值对应成一个ascii符
# 也就实现了rgb -> ascii
# 当像素透明时,直接返回一个空白字符串
if alpha == 0:
return ' '
length = len(ASCII_CHAR)
gray = int(0.299 * r + 0.587 * g + 0.114 * b)
# 灰度值和字符串的对应关系
# 每个字符串对应灰度值的区间是
unit = (256.0 + 1)/length
# 找到灰度值所对应字符串的下标
index = int(gray/unit)
return ASCII_CHAR[index]
def image_to_ascii_chart(image):
width, height = image.size
text = ''
for y in range(0,height,10):
line = ''
for x in range(0,width,5):
# 找到对应位置的像素点
dot = image.getpixel((x, y))
line += rgb_to_ascii(*dot)
text += line
text += '\n'
return text
# 每个图转化为字符串
files = os.listdir('images')
xxx = []
for i in range(0,len(files)):
pic = Image.open(f'images/{i}.png').convert('RGB')
xxx.append(image_to_ascii_chart(pic))
def Start():
os.system("clear")
print('Press any key...')
input()
os.system("clear")
i=0
print(xxx[i])
c = input()
while c != 'q':
os.system("clear")
print(xxx[i])
c = input()
i=i+1
print('End')
Start()