作者 |FrigidWinter
来源 | CSDN博客
放鞭炮贺新春,在我国有两千多年历史。关于鞭炮的起源,有个有趣的传说。
西方山中有焉,长尺余,一足,性不畏人。犯之令人寒热,名曰年惊惮,后人遂象其形,以火药为之。——《神异经》
def video2Pic(vp):
number = 0
if vp.isOpened():
r,frame = vp.read()
if not os.path.exists('cachePic'):
os.mkdir('cachePic')
os.chdir('cachePic')
else:
r = False
while r:
number += 1
cv2.imwrite(str(number)+'.jpg',frame)
r,frame = vp.read()
os.chdir("..")
returnnumber
def color2Char(r,g,b,alpha = 256):
imgChar= list("#RMNHQODBWGPZ*@$C&98?32I1>!:-;. ")
if alpha:
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
unit = 256 / len(imgChar)
return imgChar[int(gray / unit)]
else:
return''
img = Image.open(imagePath).convert('RGB').resize((imgWidth, imgHeight),Image.NEAREST)
for i in range(imgHeight):
for j in range(imgWidth):
pixel = img.getpixel((j, i))
color.append((pixel[0],pixel[1],pixel[2]))
txt = txt + color2Char(pixel[0], pixel[1], pixel[2], pixel[3]) if len(pixel) == 4 else \
txt + color2Char(pixel[0], pixel[1], pixel[2])
txt += '\n'
color.append((255,255,255))
def img2Video(vp, number, savePath):
videoFourcc = VideoWriter_fourcc(*"MP42") # 设置视频编码器
asciiImgPathList = ['cacheChar' + r'/{}.jpg'.format(i) for i in range(1, number + 1)]
asciiImgTemp = Image.open(asciiImgPathList[1]).size
videoWritter= VideoWriter(savePath, videoFourcc, vp.get(cv2.CAP_PROP_FPS), asciiImgTemp)
for imagePath in asciiImgPathList:
videoWritter.write(cv2.imread(imagePath))
videoWritter.release()
import cv2
from PIL import Image,ImageFont,ImageDraw
import os
from cv2 import VideoWriter, VideoWriter_fourcc
'''
* @breif: 将像素颜色转换为ASCII字符
* @param[in]: 像素RGBA值
* @retval: 字符
'''
def color2Char(r,g,b,alpha = 256):
imgChar = list("#RMNHQODBWGPZ*@$C&98?32I1>!:-;. ")
if alpha:
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
unit = 256 / len(imgChar)
return imgChar[int(gray / unit)]
else:
return ''
'''
* @breif: 将视频逐帧转换为图片
* @param[in]: vp -> openCV视频句柄
* @retval: number -> 转换的图片数
'''
def video2Pic(vp):
number = 0
if vp.isOpened():
r,frame = vp.read()
if not os.path.exists('cachePic'):
os.mkdir('cachePic')
os.chdir('cachePic')
else:
r = False
while r:
number += 1
cv2.imwrite(str(number)+
'.jpg' ,frame)r,frame = vp.read()
os.chdir("..")
return number
'''
* @breif: 将图片逐像素转换为ASCII字符
* @param[in]: imagePath -> 图片路径
* @param[in]: index -> 图片索引
* @retval: None
'''
def img2Char(imagePath, index):
# 初始化
txt, color, font = '', [], ImageFont.load_default().font
imgWidth, imgHeight = Image.open(imagePath).size
asciiImg = Image.new("RGB",(imgWidth, imgHeight), (255,255,255))
drawPtr = ImageDraw.Draw(asciiImg)
imgWidth, imgHeight = int(imgWidth / 6), int(imgHeight / 15)
# 对图像帧逐像素转化为ASCII字符并记录RGB值
img = Image.open(imagePath).convert('RGB').resize((imgWidth, imgHeight),Image.NEAREST)
for i in range(imgHeight):
for j in range(imgWidth):
pixel = img.getpixel((j, i))
color.append((pixel[0],pixel[1],pixel[2]))
txt = txt + color2Char(pixel[0], pixel[1], pixel[2], pixel[3]) if len(pixel) == 4 else \
txt + color2Char(pixel[0], pixel[1], pixel[2])
txt += '\n'
color.append((255,255,255))
# 绘制ASCII字符画并保存
x, y = 0,0
fontW, fontH = font.getsize(txt[1])
fontH *= 1.37
for i in range(len(txt)):
if(txt[i]=='\n'):
x += fontH
y = -fontW
drawPtr.text((y,x), txt[i], fill=color[i])
y += fontW
os.chdir('cacheChar')
asciiImg.save(str(index)+'.jpg')
os.chdir("..")
'''
* @breif: 将视频转换为ASCII图像集
* @param[in]: number -> 帧数
* @retval: None
'''
def video2Char(number):
if not os.path.exists('cacheChar'):
os.mkdir('cacheChar')
img_path_list = ['cachePic' + r'/{}.jpg'.format(i) for i in range(1, number + 1)]
task = 0
for imagePath in img_path_list:
task += 1
img2Char(imagePath, task)
'''
* @breif: 将图像合成视频
* @param[in]: vp -> openCV视频句柄
* @param[in]: number -> 帧数
* @param[in]: savePath -> 视频保存路径
* @retval: None
'''
def img2Video(vp, number, savePath):
videoFourcc = VideoWriter_fourcc(*"MP42") # 设置视频编码器
asciiImgPathList = ['cacheChar' + r'/{}.jpg'.format(i) for i in range(1, number + 1)]
asciiImgTemp = Image.open(asciiImgPathList[1]).size
videoWritter= VideoWriter(savePath, videoFourcc, vp.get(cv2.CAP_PROP_FPS), asciiImgTemp)
for imagePath in asciiImgPathList:
videoWritter.write(cv2.imread(imagePath))
videoWritter.release()
if __name__ == '__main__':
videoPath = 'test.mp4'
savePath = 'new.avi'
vp = cv2.VideoCapture(videoPath)
number = video2Pic(vp)
video2Char(number)
img2Video(vp, number, savePath)
vp.release()
分享
点收藏
点点赞
点在看
文章转发自AI科技大本营微信公众号,版权归其所有。文章内容不代表本站立场和任何投资暗示。
Copyright © 2021.Company 元宇宙YITB.COM All rights reserved.元宇宙YITB.COM