過年了鞭炮放起來,氣氛整到位!100行 Python 程式碼製作動態鞭炮
尊重原創版權: https://www.gewuweb.com/hot/17874.html
過年了鞭炮放起來,氣氛整到位!100行 Python 程式碼製作動態鞭炮
0 寫在前面
放鞭炮賀新春,在我國有兩千多年曆史。關於鞭炮的起源,有個有趣的傳說。
西方山中有焉,長尺餘,一足,性不畏人。犯之令人寒熱,名曰年驚憚,後人遂象其形,以火藥為之。——《神異經》
當初人們燃竹而爆,是為了驅嚇危害人們的山魈。據說山魈最怕火光和響聲,所以每到除夕,人們便“燃竹而爆”,把山魈嚇跑。這樣年復一年,便形成了過年放鞭炮、點紅燭、敲鑼打鼓歡慶新春的年俗。
新年新氣象,今天就用程式碼來製作一個 動態鞭炮 ,效果如下所示。
動態鞭炮的基本原理是:將一個錄製好的鞭炮視訊以字元畫的形式復現,基本步驟是幀取樣 → 逐幀轉換為字元畫 → 字元畫合成視訊。下面開始吧!
1 視訊幀取樣
函式如下所示,主要功能是將視訊的影象流逐幀儲存到特定的快取資料夾中(若該資料夾不存在會自動建立)。函式輸入vp是openCV視訊控制代碼,輸出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
2 將圖片轉為字元畫
2.1 建立畫素-字元索引
函式輸入畫素RGBA值,輸出對應的字元碼。其原理是將字元均勻地分佈在整個灰度範圍內,畫素灰度值落在哪個區間就對應哪個字元碼。字元碼可以參考 ASCII碼
ASCII 碼使用指定的7 位或8 位二進位制數組合來表示128 或256 種可能的字元。標準ASCII 碼也叫基礎ASCII碼,使用7
位二進位制數(剩下的1位二進位制為0)來表示所有的大寫和小寫字母,數字0
到9、標點符號,以及在美式英語中使用的特殊控制字元。其中:0~31及127(共33個)是控制字元或通訊專用字元(其餘為可顯示字元),如控制符:LF(換行)、CR(回車)、FF(換頁)、DEL(刪除)、BS(退格)、BEL(響鈴)等;通訊專用字元:SOH(文頭)、EOT(文尾)、ACK(確認)等;ASCII值為8、9、10
和13 分別轉換為退格、製表、換行和回車字元。它們並沒有特定的圖形顯示,但會依不同的應用程式,而對文字顯示有不同的影響。
RGBA是代表Red(紅色)、Green(綠色)、Blue(藍色)和Alpha的色彩空間,Alpha通道一般用作不透明度引數。如果一個畫素的alpha通道數值為0%,那它就是完全透明的,而數值為100%則意味著一個完全不透明的畫素(傳統的數字影象)。gray=0.2126
-
r + 0.7152 * g + 0.0722 * b是RGB轉為灰度值的經驗公式,人眼對綠色更敏感。
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 ''
2.2 將圖片逐畫素轉換為字元
核心程式碼如下,遍歷圖片的每個畫素
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))
3 將字元影象合成視訊
輸入引數vp是openCV視訊控制代碼,number是幀數,savePath是視訊儲存路徑,函式中 MP42
是可以生成較小並且較小的視訊檔案的編碼方式,其他類似的還有isom、mp41、avc1、qt等,表示“最好”基於哪種格式來解析當前的檔案。
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()
4 完整程式碼
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()
最後
總有起風的清晨,總有溫暖的午後,總有燦爛的黃昏,總有流星的夜晚,總有一個人在祈禱世界上所有的美好全部屬於你們!祝小夥伴們虎年快樂,心想事成!