只需要在最后一行括号填入视频路径即可文章来源地址https://www.toymoban.com/news/detail-508608.html
import os
from PIL import Image, ImageDraw, ImageFont
import cv2
import time
from tqdm import tqdm
import threading
if __name__ == '__main__':
# path = r'img\sportball\0.jpg'
def cv2_pit_imgchar(path,save_path=''):
def create_img(text,show='no',save_path=save_path):#创建图片,将文本写入到图片
s = time.time()
img = Image.new('RGB', (1260, 915), color = (255, 255, 255))#创建一张图片
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('SIMLI.TTF', 10)#字体,文字大小
draw.text((1,0),text, font=font, fill=(0, 0, 0))#起始坐标,字符串,字体,字体颜色
e = time.time()
# print(f'创建图片写入字符耗时{round(e-s,2)}s')
if show=='yes':#如果show为yes,
img.show()
if not save_path=='':#如果路径不为空,保存图片
img.save(save_path)
return img
ASCII_CHARS = ['.',',',':',';','+','*','?','T','P','%','@']
# ascii_chars = "MNHQ$OC67+>!:-. "
# ASCII_CHARS=list(ascii_chars)
ASCII_CHARS = ASCII_CHARS[::-1]#反转
img1 = cv2.imread(path,cv2.IMREAD_GRAYSCALE)#读取图片并转为灰度图
def cv2_img_resize(img1,w_size):#缩放图片
h,w = img1.shape[0:2]#获取图片高宽
# print(f"图片原始高度{h}宽度{w}")
re_rate=h/w#缩放比例
# print(f'比例{re_rate}')
img1=cv2.resize(img1,(w_size,int(w_size*re_rate/2)))#缩放图片,因为字符宽高不一致需要压缩一半图片高度
return img1
img1 = cv2_img_resize(img1,250)
def img_to_char(img1):#将图片按灰度值转为字符
img_text = ''
s1 = time.time()
for y in img1:
for x in y:
new_pixels = [ASCII_CHARS[x//25]]
new_pixels= ''.join(new_pixels)
img_text+=str(new_pixels)
# print(x,end=',')
img_text+='\n'
e1 = time.time()
# print(f'读取全图灰度值耗时{round(e1-s1,2)}s')
return img_text
img_text = img_to_char(img1)
create_img(img_text)#创建图片
# cv2_pit_imgchar(path)
def get_folder_imgs(folder):#读取获得文件夹下所有jpg文件路径,返回图片路径列表
x=0
path_list = []
for img_name in os.listdir(folder):
if '.jpg' in img_name:
# print(img_name)
path_list.append(folder+img_name)
x+=1
print(f'总共{x}张图片')
return path_list
def video_to_imgs(video_path = r"D:\1111\sportballcn.mp4",save_path='img/sportball/',th=4):#将视频拆成图片
video = cv2.VideoCapture(video_path)
fps = int(video.get(cv2.CAP_PROP_FPS)) # 获取视频每秒的帧数
if not os.path.exists(save_path):
print('创建保存路径')
os.makedirs(save_path)
def run(t,th):
video = cv2.VideoCapture(video_path)
frames = video.get(cv2.CAP_PROP_FRAME_COUNT)#获取视频总帧数
print(f'第{t+1}个线程启动,处理{int(frames*t/th)}到{int(frames*(t+1)/th)}帧')
t1 = time.time()
for x in tqdm(range(int(frames))):
retval,image = video.read()
if not retval:
break
if frames*t/th<=x<=frames*(t+1)/th:
cv2.imwrite(f'{save_path}{x}.jpg',image)
t2 = time.time()
print(f"线程{t+1},耗时{round(t2-t1,2)}s")
tt = dict()
for t in range(th):
tt[t]=threading.Thread(target=run,args=(t,th))
for t in range(th):
tt[t].start()
for t in range(th):
tt[t].join()
#print('\n\n\n\n\n\n\naaaaaaaaaaaa')
return fps
def batch_img_to_charimg(folder='img/sportball/',save_path='img/spaortchar/',th=1):#批量将图片转为字符图
path_list = get_folder_imgs(folder)
if not os.path.exists(save_path):
print(f'创建字符图保存路径{save_path}')
os.makedirs(save_path)
print('正在将图片转换为字符图')
img_counting = len(path_list)
def run(t,th):
t1=time.time()
print(f'第{t+1}个线程启动,处理{int(img_counting*t/th)}到{int(img_counting*(t+1)/th)}张图')
x=0
for path in tqdm(path_list):
name = path.split('/')[-1]
if img_counting*t/th<=x<=img_counting*(t+1)/th:
cv2_pit_imgchar(path=path,save_path=f'{save_path}{name}')
x+=1
t2 = time.time()
print(f"线程{t+1},耗时{round(t2-t1,2)}s")
tt=dict()
for t in range(th):
tt[t]=threading.Thread(target=run,args=(t,th))
for t in range(th):
tt[t].start()
for t in range(th):
tt[t].join()
def char_imgs_to_video(char_img_folder = "img/spaortchar",save_name='streetchar.mp4',fps=30): #将所有字符图合成视频 #包含所有字符图的路径
fps=fps # 每秒帧数,最好跟原视频一致
size = (1260, 915) # (宽, 高) 必须跟图片一致
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 编码为 mp4v 格式
char_video = cv2.VideoWriter(save_name, fourcc, fps, size, isColor=True)
img_names = os.listdir(char_img_folder)
img_names.sort(key=lambda x:int(x[:-4]))#进行排序
print('将字符图合并成视频中...')
for img_name in tqdm(img_names):
if '.jpg' in img_name:
# print(char_img_folder+'/'+img_name)
img = cv2.imread(char_img_folder+'/'+img_name)
char_video.write(img)#将图片添加视频
char_video.release()
print('完成')
def batch_img_to_charimg1(folder='img/aq/',save_path='img/aqchar/'):#批量将图片转为字符图
path_list = get_folder_imgs(folder)
if not os.path.exists(save_path):
print(f'创建字符图保存路径{save_path}')
os.makedirs(save_path)
print('正在将图片转换为字符图')
for path in tqdm(path_list):
name = path.split('/')[-1]
cv2_pit_imgchar(path=path,save_path=f'{save_path}{name}')
def v2cv(video_path = r"D:\1111\aq.mp4"):#video_path需要转换的视频路径
s_p1 = 'img/img/'#图片保存路径
s_p2 = 'img/charimg/'#字符图保存路径
fps=30
# 第一步,将视频拆成图片
fps=video_to_imgs(video_path=video_path,save_path=s_p1,th=4)
# 第二部,将图片批量转换成字符图
batch_img_to_charimg(folder=s_p1,save_path=s_p2,th=1)#多线程很慢是啥情况
# 第三步,将字符图合成视频
char_imgs_to_video(char_img_folder = s_p2,save_name='char.mp4',fps=fps)
v2cv('d:/1111/sd.mp4')
文章来源:https://www.toymoban.com/news/detail-508608.html
到了这里,关于python 视频转字符视频 bate的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!